众所周知,Nginx 是 Apache服务不错的替代品。其特色是占有内存少,并发能力强,事实上 Nginx 的并发能力在同类型的网页服务器中表现较好,所以国内知名大厂例如:淘宝,京东,百度,新浪,网易,腾讯等等都在使用Nginx网站。javascript
Nginx 是开源、高性能、高可靠的 Web 和反向代理服务器,并且支持热部署,同时也提供了 IMAP/POP3/SMTP 服务,能够不间断运行,提供热更新功能。占用内存少、并发能力强,最重要的是,Nginx 是免费的并能够商业化,配置使用都比较简单。css
因此,今天民工哥就给你们整理一份 Nginx的经常使用配置清单,供你们学习与生产配置参考使用。主要包括如下三个方面:html
./configure --without-module1 --without-module2 --without-module3例如:./configure --without-http_dav_module --withouthttp_spdy_module#注意事项:配置指令是由模块提供的。确保你禁用的模块不包含你须要使用的指令!在决定禁用模块以前,应该检查Nginx文档中每一个模块可用的指令列表。
worker_processes 8;#Nginx 进程数,建议按照CPU数目来指定,通常为它的倍数 (如,2个四核的CPU计为8)。worker_rlimit_nofile 65535; #一个Nginx 进程打开的最多文件描述符数目worker_connections 65535;#每一个进程容许的最多链接数
server { listen 80; #监听端口 server_name www.mingongge.com; #域名信息 location / { root /www/www; #网站根目录 index index.html index.htm; #默认首页类型 deny 192.168.2.11; #禁止访问的ip地址,能够为all allow 192.168.3.44; #容许访问的ip地址,能够为all } }
小技巧补充:域名匹配的四种写法nginx
精确匹配:server_name www.mingongge.com ; 左侧通配:server_name *.mingongge.com ; 右侧统配:server_name www.mingongge.* ; 正则匹配:server_name ~^www\.mingongge\.*$ ; 匹配优先级:精确匹配 > 左侧通配符匹配 > 右侧通配符匹配 > 正则表达式匹配
[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf … … location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } … … [root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
error_log /var/log/nginx/error.log warn; #配置错误日志的级别及存储目录 events { worker_connections 1024; } http { .................. log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #配置日志的模式 access_log /var/log/nginx/access.log main; #配置访问日志存储目录 }
以上配置只是Nginx自身关于日志的基本配置,在实际生产环境中,咱们须要收集日志、分析日志,才定更好的去定位问题,推荐给你们:超强干货!经过filebeat、logstash、rsyslog 几种方式采集 nginx 日志web
http { sendfile on #高效传输文件的模式 必定要开启 keepalive_timeout 65 #客户端服务端请求超时时间 }
server { listen 80; server_name mingongge.com; location /static { root /wwww/web/web_static_site; } }
也可使用下面的方法正则表达式
location /image { alias /web/nginx/static/image/; } 注意:使用alias末尾必定要添加/,而且它只能位于location中
好比生产环境(同一台服务中)有不一样的项目,这个就比较实用了,用反向代理去作请示转发。json
http { ............. upstream product_server{ 127.0.0.1:8081; } upstream admin_server{ 127.0.0.1:8082; } upstream test_server{ 127.0.0.1:8083; } server { #默认指向product的server location / { proxy_pass http://product_server; } location /product/{ proxy_pass http://product_server; } location /admin/ { proxy_pass http://admin_server; } location /test/ { proxy_pass http://test_server; } } }
更多关于 Nginx 实践:location 路径匹配后端
upstream server_pools { server 192.168.1.11:8880 weight=5; server 192.168.1.12:9990 weight=1; server 192.168.1.13:8989 weight=6; #weigth参数表示权值,权值越高被分配到的概率越大 } server { listen 80; server_name mingongge.com; location / { proxy_pass http://server_pools; } }
proxy_connect_timeout 90; #nginx跟后端服务器链接超时时间(代理链接超时) proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) proxy_read_timeout 90; #链接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 4k; #代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小 proxy_set_header Host $host; proxy_set_header X-Forwarder-For $remote_addr; #获取客户端真实IP
location / { return 404; #直接返回状态码 } location / { return 404 "pages not found"; #返回状态码 + 一段文本 } location / { return 302 /blog ; #返回状态码 + 重定向地址} location / { return https://www.mingongge.com ; #返回重定向地址 }
示例以下浏览器
server { listen 80; server_name www.mingongge.com; return 301 http://mingongge.com$request_uri; } server { listen 80; server_name www.mingongge.com; location /cn-url { return 301 http://mingongge.com.cn; } } server{ listen 80; server_name mingongge.com; # 要在本地hosts文件进行配置 root html; location /search { rewrite ^/(.*) https://www.mingongge.com redirect; } location /images { rewrite /images/(.*) /pics/$1; } location /pics { rewrite /pics/(.*) /photos/$1; } location /photos { } }
这样的设置能够阻止缓冲区溢出攻击(一样是Server模块)
client_body_buffer_size 1k; client_header_buffer_size 1k; client_max_body_size 1k; large_client_header_buffers 2 1k; #设置后,无论多少HTTP请求都不会使服务器系统的缓冲区溢出了
在http模块内server模块外配置limit_conn_zone,配置链接的IP,在http,server或location模块配置limit_conn,能配置IP的最大链接数。
limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 1;
gzip_types #压缩的文件类型 text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript gzip on; #采用gzip压缩的形式发送数据 gzip_disable "msie6" #为指定的客户端禁用gzip功能 gzip_static; #压缩前查找是否有预先gzip处理过的资源 gzip_proxied any; #容许或者禁止压缩基于请求和响应的响应流 gzip_min_length 1000; #设置对数据启用压缩的最少字节数 gzip_comp_level 6; #设置数据的压缩等级
open_file_cache #指定缓存最大数目以及缓存的时间 open_file_cache_valid #在open_file_cache中指定检测正确信息的间隔时间 open_file_cache_min_uses #定义了open_file_cache中指令参数不活动时间期间里最小的文件数 open_file_cache_errors #指定了当搜索一个文件时是否缓存错误信息 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #指定缓存文件的类型 { expires 3650d; #指定缓存时间 } location ~ .*\.(js|css)?$ { expires 3d; }
server { listen 192.168.1.250:443 ssl; server_tokens off; server_name mingonggex.com www.mingonggex.com; root /var/www/mingonggex.com/public_html; ssl_certificate /etc/nginx/sites-enabled/certs/mingongge.crt; ssl_certificate_key /etc/nginx/sites-enabled/certs/mingongge.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; } # Permanent Redirect for HTTP to HTTPS server { listen 80; server_name mingongge.com; return 301 https://$server_name$request_uri; }
location / { mirror /mirror; proxy_pass http://backend; } location = /mirror { internal; proxy_pass http://test_backend$request_uri; }
流量限制配置两个主要的指令,limit_req_zone
和limit_req
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; server { location /login/ { limit_req zone=mylimit; proxy_pass http://my_upstream; } }
更多、更详细的限流配置请参考:葵花宝典!一文搞定 Nginx 限流配置
server_tokens在打开的状况下会使404页面显示Nginx的当前版本号。这样作显然不安全,由于黑客会利用此信息尝试相应Nginx版本的漏洞。只须要在nginx.conf中http模块设置server_tokens off便可,例如:
server { listen 192.168.1.250:80; Server_tokens off; server_name mingongge.com www.mingongge.com; access_log /var/www/logs/mingongge.access.log; error_log /var/www/logs/mingonggex.error.log error; root /var/www/mingongge.com/public_html; index index.html index.htm; } #重启Nginx后生效:
User Agent是HTTP协议中对浏览器的一种标识,禁止非法的User Agent能够阻止爬虫和扫描器的一些请求,防止这些请求大量消耗Nginx服务器资源。
为了更好的维护,最好建立一个文件,包含不指望的user agent列表例如/etc/nginx/blockuseragents.rules包含以下内容:
map $http_user_agent $blockedagent { default 0; ~*malicious 1; ~*bot 1; ~*backdoor 1; ~*crawler 1; ~*bandit 1; }
而后将以下语句放入配置文件的server模块内
include /etc/nginx/blockuseragents.rules; 并加入if语句设置阻止后进入的页面:
location /img/ { valid_referers none blocked 192.168.1.250; if ($invalid_referer) { return 403; } }
一些web站点和应用,能够只支持GET、POST和HEAD方法。在配置文件中的 serve r模块加入以下方法能够阻止一些欺骗攻击
if ($request_method !~ ^(GET|HEAD|POST)$) { return 444; }
尽可能避免使用SSL,要用TLS替代,如下配置能够放在Server模块内
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
经过这一系列的配置以后,相信你的Nginx服务器足够应付实际生产需求了。
也欢迎你们积极留言补充这份经常使用配置清单,以便它更完整、更完善。