Nginx最佳安装与优化
wget http://nginx.org/download/nginx-1.11.13.tar.gz tar zxvf nginx-1.11.13.tar.gz cd nginx-1.11.13 yum -y install pcre pcre-devel gcc* ncurser-devel perl openssl-devel whereis cc gcc ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module
//--with-pcre在Nginx添加对正则表达式的支持
//--with-http_stub_status_module添加对状态页面的支持
//--with-http_ssl_module在Nginx添加对https站点的支持
make make install
修改nginx.conf配置如下: vi /usr/local/nginx/conf/nginx.conf worker_processes 1; 改成 worker_processes 8; 新增 worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; #error_log logs/error.log; 去掉# error_log logs/error.log; #pid logs/nginx.pid; 去掉# pid logs/nginx.pid; events { worker_connections 1024; 改成 events { use epoll; worker_connections 10000; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; 去掉# log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #tcp_nopush on; 去掉# tcp_nopush on; #gzip on; 去掉# gzip on; 新增 upstream www {
keepalive 50; #必须配置,建议值50-100 server 10.8.8.1; server 10.8.8.2; } #charset koi8-r; #access_log logs/host.access.log main; 去掉# charset koi8-r; access_log logs/host.access.log main; location / { root html; index index.html index.htm; } 改成 location / {
proxy_http_version 1.1; #后端配置支持HTTP 1.1;必须。
proxy_set_header Connection ""; #后端配置支持HTTP 1.1;必须。 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 3; proxy_send_timeout 3; proxy_read_timeout 3; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; proxy_max_temp_file_size 128m; proxy_pass http://www; }
增加性能采集: location /ngx_status { stub_status on; access_log off; allow 127.0.0.1; #请对应增加需要访问的IP来源 deny all; }