设为首页 收藏本站
查看: 849|回复: 0

[经验分享] Nginx作为web服务器

[复制链接]

尚未签到

发表于 2017-12-22 23:20:06 | 显示全部楼层 |阅读模式
为什么选择Nginx,nginx有诸多优点:
  nginx是轻量级web服务器,支持AIO、mmap、event-driven,解决了c10k问题、虚拟主机、基于名字和IP访问、nginx平滑升级 、热部署、自定义日志格式、3XX 5XX错误代码重定向、重写url、支持http referer 使用防盗链(判断请求过来的url)、支持flv和mp4流、速度限制。

框架:
  一个主进程master,多个子进程worker
  支持sendfile/sendfile64

配置文件结构:
  server{}:虚拟主机
  location{}:
  location /URI/ {
  root “/web/htdocs”
  }
  location URI:对当前路径和自路径下的所有对象都生效,匹配范围大
  location = URI:对当前路径生效,精确匹配指定路径,不包括子路径
  location ~ URI {}:
  location ~* URI {}:
  模式匹配URI,此处的URI可以使用正则表达式,~区分字符大小写,~*不区分
  location ^~ URI {}:不使用正则表达式
  匹配优先级”=” > “/”  “^~” > “~”
例子:

  

location = / {  

[ configuration A ]

  

  
}
  

  
location / {
  

[ configuration B ]

  

  
}
  

  
location /documents/ {
  

[ configuration C ]

  

  
}
  

  
location ^~ /images/ {
  

[ configuration D ]

  

  
}
  

  
location ~* \.(gif|jpg|jpeg)$ {
  

[ configuration E ]

  

  
}
  

The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.



  •   一般配置

  

location / {  

  root   /web/web1;
  

  index  index.html index.htm;
  

  
}
  

DSC0000.png

  

  location /web2/ {  

  root /web;
  

  index index.html index.htm;
  

  
}
  

DSC0001.png


介绍几个http相关模块:


  • 访问控制法则:基于IP,Module ngx_http_access_module
  deny
  allow
  

        location /web2/ {  

  root /web;
  

  index index.html index.htm;
  

  deny 192.168.31.85;
  

  }
  

DSC0002.png



  • 基于用户:Module ngx_http_auth_basic_module
  

location / {  

  satisfy any;
  

  allow 192.168.1.0/32;
  

  deny  all;
  

  

  auth_basic           "closed site";
  

  auth_basic_user_file conf/htpasswd;
  

  
}
  

  
location /web2/ {
  

  root /web;
  

  index index.html index.htm;
  

  auth_basic "closed site";
  

  auth_basic_user_file /etc/nginx/.users;
  

  
}
  

  创建用户:
  -c 第一次创建文件时使用:
  # htpasswd -c -m /etc/nginx/.users tom
  # htpasswd -m /etc/nginx/.users tom2
DSC0003.png



  • 允许列出其文件:Module ngx_http_autoindex_module
  找不到主页面时,默认拒绝访问:
  

location / {  

  root   /web/web1;
  

  index home.html;
  

  }
  

DSC0004.png

  

location / {  

  root   /web/web1;
  

  index home.html;
  

  autoindex on;
  

  
}
  

DSC0005.png



  • 统计访问信息:Module ngx_http_stub_status_module
  

location /basic_status {  

  stub_status;
  

  
}
  

  In versions prior to 1.7.5, the directive syntax required an arbitrary argument, for example, “stub_status on”.
  访问:http://192.168.2.168/basic_status
DSC0006.png

  当前处理活动的链接个数
  已经接受的连接数  已经处理的连接数  已经处理的请求数
Reading

The current number of connections where nginx is reading the request header.

Writing

The current number of connections where nginx is writing the response back to the client.

Waiting


The current number of>

  • https访问:Module ngx_http_ssl_module
  

server {  

  listen       443 ssl;
  

  server_name  localhost;
  

  

  ssl                  on;
  

  ssl_certificate      /etc/nginx/ssl/nginx.crt;
  

  ssl_certificate_key  /etc/nginx/ssl/nginx.key;
  

  

  ssl_session_cache    shared:SSL:1m;
  

  ssl_session_timeout  5m;
  

  

  ssl_ciphers  HIGH:!aNULL:!MD5;
  

  ssl_prefer_server_ciphers  on;
  

  

  location / {
  

  root   /web/ssl;
  

  index  index.html index.htm;
  

  }
  

  }
  

DSC0007.png



  • Module ngx_http_fastcgi_module
  

location ~ ^(.+.php)(.*)$ {  

  
fastcgi_split_path_info ^(.+.php)(.*)$;
  

  
include fastcgi.conf;
  

  
fastcgi_pass 127.0.0.1:9000;
  

  
fastcgi_index index.php;
  

  
fastcgi_param PATH_INFO $fastcgi_path_info;
  

  
}
  

  参见前面Zabbix系统搭建,就是一个很好LEMP应用的实践。

总结:
  Nginx以其优秀的性能和提供的各种模块,作为web服务器越来越被多数企业选择,这里没有详细介绍Nginx作为web服务器相关的各个模块,大家可以参考官方文档或其他途径学习更多Nginx特性,官方文档http://nginx.org/en/docs/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-427010-1-1.html 上篇帖子: nginx 错误日志分析 下篇帖子: 使用uWSGI+Nginx+Supervisor部署管理Django应用程序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表