1 2 3 4 5 6 7 8 | [root@db02 ~]# curl -I http://www.lichengbing.cn HTTP/1.1 200 OKServer: nginx/1.6.3Date: Tue, 16 Aug 2016 14:39:48 GMTContent-Type: text/html; charset=UTF-8Connection: keep-aliveX-Powered-By: PHP/5.5.32Link: <http://www.lichengbing.cn/wp-json/>; rel="https://api.w.org/" |
1 2 3 4 5 6 7 8 | http { ... server_tokens off; #控制http response header内的服务版本信息的显示,以及错误信息中web服务版本信息 ...}[root@db02 ~]# curl -I http://www.lichengbing.cnHTTP/1.1 200 OKServer: nginx #版本隐藏 |
1 2 3 4 5 6 | [root@lichengbing nginx-1.6.3]# cd ~/tools/nginx-1.6.3[root@lichengbing nginx-1.6.3]# sed -n '13,17p' src/core/nginx.h #define NGINX_VERSION "1.6.3" #改成你想要的版本号,如2.2.5#define NGINX_VER "Nginx/" NGINX_VERSION #你想改成的软件名称,如Apache#define NGINX_VAR "NGINX" #可以改成OWS等#define NGX_OLDPID_EXT ".oldbin" |
1 | [root@lichengbing nginx-1.6.3]# sed -i 's#Server: nginx#Server: OWS#g' src/http/ngx_http_header_filter_module.c |
1 2 3 4 5 6 7 8 9 | [root@lichengbing nginx-1.6.3]# sed -n '21,30p' src/http/ngx_http_special_response.c static u_char ngx_http_error_full_tail[] ="<hr><center>" NGINX_VER "</center>" CRLF"</body>" CRLF"</html>" CRLF;static u_char ngx_http_error_tail[] ="<hr><center>nginx</center>" CRLF"</body>" CRLF |
1 2 3 4 5 6 7 8 9 | [root@lichengbing nginx-1.6.3]# sed -n '21,30p' src/http/ngx_http_special_response.c static u_char ngx_http_error_full_tail[] ="<hr><center>" NGINX_VER " (http://www.lichengbing.cn)</center>" CRLF"</body>" CRLF"</html>" CRLF;static u_char ngx_http_error_tail[] ="<hr><center>OWS</center>" CRLF"</body>" CRLF |
1 2 3 4 5 6 7 8 9 10 | [root@lichengbing ~]# grep '#user' /application/nginx/conf/nginx.conf.default #user nobody; #系统默认的用户为nobody[root@lichengbing ~]# useradd www -s /sbin/nologin -M #建立用户wwwuser www www; #nginx.conf 中配置用户也可以在编译时加入用户和用户组,如--user=www --group=www --with-http_ssl_module --with-http_stub_status_module --prefix=/application/nginx-1.6.3/[root@lichengbing ~]# ps -ef|grep nginx|grep -v greproot 1386 1 0 Jul21 ? 00:00:00 nginx: master process /application/nginx/sbin/nginxwww 24732 1386 0 23:19 ? 00:00:00 nginx: worker process#此时worker process进程就是使用的www用户权限,当然也可以使master降权运行 |
1 2 3 4 5 6 7 | worker_processes 8; #最好为服务器CPU的逻辑核心数[root@netmonitor ~]# grep "physical id" /proc/cpuinfo|sort|uniq|wc -l #物理核数2[root@netmonitor ~]# grep "cpu cores" /proc/cpuinfo|uniq #单核心数cpu cores : 4[root@netmonitor ~]# grep "processor" /proc/cpuinfo|wc -l #逻辑核心数8 |
1 2 3 | worker_processes 8;worker_cpu_affinity 001 0010 0100 1000; #数字代表1、2、3、4的掩码,平均分摊进程压力worker_cpu_affinity 00000001 00000010...#8核心写法 |
1 | taskset -c 1,2,3 /application/nginx/sbin/nginx start |
1 2 3 | events { use epoll; #nginx官方建议,可以不指定事件处理模型,Nginx会自动选择最佳的事件处理模型} |
1 2 3 4 5 6 7 | worker_processes 8;events { worker_connections 1024;}#最大连接数Max_client = worker_processes * worker_connections#进程的最大连接数收Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -HSn 65535”后,才能生效#连接数并不是越大越好,要在系统性能能接受的范围内 |
1 2 3 | events { worker_rlimit_nofile 65535; #该值可设置为系统优化后的ulimit -HSn的结果} |
1 2 3 4 | http { sendfile on; #开启文件的高效传输模式}#同时将tcp_nopush和tcp_nodelay两个指令设置为on,减少网络传输包数量、防止网络及磁盘的I/O阻塞,提升Nginx效率 |
1 2 3 4 5 6 7 8 | http { keepalive_timeout 65; #设置客户端连接保持会话的超时时间 tcp_nodelay on; #激活tcp_nodelay,提高I/O性能,在包含keepalive参数时才有效 client_header_timeout 15; #读取客户端请求头数据超时时间,超时则返回“Request time out(408)” clietn_body_timeout 15; #读取客户端请求主体超时时间 send_timeout 15; #指定客户端响应时间}#将无用的连接尽快设置为超时,可以保护服务器的系统资源(CPU、内存、磁盘) |
1 2 3 | http { client_max_body_size 10m; #具体数字要根据业务需求决定} |
1 2 3 4 5 6 7 8 9 10 | http { fastcgi_connect_timeout 240; #连接到后端fastcgi的超时时间 fastcgi_send_timeout 240; #已经和fastcgi建立连接后多久不传送数据,就会被断开 fastcgi_read_timeout 240; #接收fastcgi应答的超时时间 fastcgi_buffer_size 64k; #指定读取fastcgi应答第一部分需要多大的缓冲区,可以设置为gastcgi_buffers选项指定的缓冲区大小 fastcgi_busy_buffers_size 128k; #繁忙时的buffer,可以是fastcgi_buffer的两倍 fastcgi_temp_path /data/ngx_fcgi_tmp; 在写入fastcgi_temp_path时将用多大的数据库,默认是fastcgi_buffer的两倍,如果太小,可能会报502 Bad GateWay fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; #指定相关参数配比} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | server { location ~ .*\.(php|php5)?$ { root html/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; fastcgi_cache ngx_fcgi_cache;#表示开启缓存并为其指定一个名称,开启缓存非常有用,可以有效降低cpu的负载,并且防止502的发生,但是也可能会带来其他问题 fastcgi_cache_valid 200 302 1h; #将对应的应答码缓存时间 fastcgi_cache_valid 301 1d; #将301缓存1天 fastcgi_cache_valid any 1m; #将其他应答缓存1分钟 fastcgi_cache_min_uses 1; #请求数量 fastcgi_cache_use_stale error timeout invalid_header http_500; #错误判断 } } |
1 2 3 4 5 6 7 8 9 | http { gzip on; #开启压缩功能 gzip_min_length 1k; #允许压缩的最小页面字节数,从header头的Content-Length中获取,不管页面多大都进行压缩,建议设置成大于1K,如果小于1K可能会越压缩越大 gzip_http_version 1.1; #压缩版本,默认为1.1,目前大部分浏览器都支持压缩 gzip_buffers 4 32k; #压缩缓冲大小,允许申请4个单位为32K的内存作为流缓存 gzip_comp_level 9; #压缩比例,1最小,9最大,传输速度最快,但是比较消耗资源 gzip_types text/css text/xml application/javascript; #指定压缩的内容类型 gzip_vary on; #vary header支持,让前端的缓存服务器继续缓存传输该压缩页面,而不提前解压} |
1 2 3 4 5 6 7 8 9 10 | server {location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #指定缓存文件的类型 { expires 3650d; #指定缓存时间 } location ~ .*\.(js|css)?$ { expires 3d; }} |
1 2 3 4 5 | #!/bin/shcd /application/nginx/logs/mv www_access.log www.access_$(date +%F -d -1day).logmv blog_access.log blog.access_$(date +%F -d -1day).log/application/nginx/sbin/nginx -s reload |
1 2 3 | cat >>/var/spool/cron/root<<EOF00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1EOF |
1 2 3 | location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ { access_log off; } |
1 2 | chown -R root.root /application/nginx/logschmod -R 700 /application/logs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | server {location ~ ^/images/.*\.(php|php5|sh|pl|py)$ { #指定目录限制访问 deny all; } location ~ ^/static/.*\.(php|php5|sh|pl|py)$ { deny all; } location ~ ^/data/.*\.(php|php5|sh|pl|py)$ { deny all; } location ~ .*\.(php|php5)?$ { #必须配置在解析之前 root html/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }} |
1 2 3 | server { location /admin/ { return 404; }} |
1 2 3 4 5 6 7 | server {location ~ ^/admin/ { allow 202.111.12.211; #allow 192.168.1.0/24; #也可以限制IP段 deny all; }} |
1 2 3 4 5 6 | if ( $remotea_addr = 10.0.0.110 ) { return 403;}if ( $remotea_addr = 10.0.0.111 ) { set $allow_access_root 'true';} |
1 2 3 4 5 6 | location / { root html/blog; index index.php index.html index.html; deny 10.0.0.7; allow all;} |
1 2 3 4 5 | server { listen 80 default_server; server_name _; return 501;} |
1 2 3 4 5 | server { listen 80 default_server; server_name _; rewrite ^(.*) http://www.lichengbing.cn$1 permanent;} |
1 2 3 4 | server { if ($host !~ ^www/.lichengbing/.com$) { rewrite ^(.*) http://www.lichengbing.cn$1 permanent;} |
1 2 3 4 5 6 7 | location ~.* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ { valid_referers none blocked *.lichengbing.cn lichengbing.cn; if ($invalid_referer) { rewrite ^/ http://www.lichengbing.cn/img/nolink.jpg }}#或者也可以使用NginxHttpAccessKeyModule实现防盗链 |
1 2 3 | server { error_page 403 /403.html; #当出现403错误时,会跳转到403.html页面} |
1 2 3 | if ($http_user_agent ~* LWP::Simple|BBBike|wget) { return 403;} |
1 2 3 4 | if ($http_user_agent ~* “qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot”) { return 403;} |
1 2 3 4 | if ($http_user_agent ~* “Firefox|MSIE”){rewrite ^(.*) http://blog.etiantian.org/$1 permanent} |
1 2 3 | if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 501;} |
1 2 3 | if ($request_method ~* ^(GET)$ ) { return 501;} |
1 2 3 4 5 6 7 | limit_conn_zone $binary_remote_addr zone=perip:10m;limit_conn_zone $server_remote_addr zone=perserver:10m;server {limit_conn perip 10;limit_conn perserver 100;}#还可以使用limit_req_zone进行控制,控制单个IP的访问速率 |

评论列表