準備工做:javascript
yum -y install gcc gcc-c++ autoconf automake makephp
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel css
以後我用的阿里的那個tengine-2.1.1.tar.gz(同事給的,我就用上了,至於有什麼特別之處,還待研究),html
groupadd nginx前端
useradd -g nginx nginxjava
以後編譯nginx,node
tar -zxvf nginx-1.9.5.tar.gznginx
cd nginx-1.9.5c++
這個有點狠,安裝了一大堆.緩存
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-pcre
make && make install
在/etc/rc.d/rc.local文件裏,寫上開機的腳本便可
/usr/local/nginx/sbin/nginx
調優:
echo 1000 >/proc/sys/net/core/somaxconn
#user nobody;
worker_processes auto;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;#Linux 下性能最好的 event 模式
worker_connections 65535;# 每一個工做進程容許最大的同時鏈接數
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
tcp_nodelay on;#這個不知道與上面的有啥不同
#keepalive_timeout 0;
keepalive_timeout 65;#長鏈接超時時間,單位是秒
client_header_buffer_size 4k;#經過getconf PAGESIZE 獲得4096,因而設置成與分頁大小同樣
open_file_cache max=102400 inactive=20s;#將爲打開文件指定緩存,默認是沒有啓用的,max指定緩存數量,建議和打開文件數一致,inactive是指通過多長時間文件沒被請求後刪除緩存
open_file_cache_valid 30s;#這個是指多長時間檢查一次緩存的有效信息
open_file_cache_min_uses 1;#open_file_cache指令中的inactive參數時間內文件的最少使用次數,若是超過這個數字,文件描述符一直是在緩存中打開的,如上例,若是有一個文件在inactive時間內一次沒被使用,它將被移除
gzip on; #開啓gzip壓縮輸出
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers 4 16k; #壓縮緩衝區
gzip_http_version 1.1; #壓縮版本(默認1.1,前端若是是squid2.5請使用1.0)
gzip_comp_level 2; #壓縮等級,9壓縮的最小可是耗費的cpu最高
gzip_types text/plain application/x-javascript text/css application/xml;
#壓縮類型,默認就已經包含text/html,因此下面就不用再寫了,寫上去也不會有問題,可是會有一個warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #開啓限制IP鏈接數的時候須要使用
upstream backend1 {
ip_hash;
server 10.4.42.219:12601 weight=10;
server 10.4.101.63:8080 weight=10;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /NginxStatus {
stub_status on; #Nginx 狀態監控配置
allow all;
#deny all;
access_log off;
}
location / {
proxy_next_upstream error timeout invalid_header http_502 http_503 http_504; #去掉404與500
proxy_read_timeout 1200s;# 設置超時時間,默認是60s,有的請求返回慢
proxy_set_header HOST $host;
proxy_set_header X-REAL-IP $remote_addr;
proxy_set_header X-FORWARD-FOR $proxy_add_x_forwarded_for;
proxy_pass http://backend1/;
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
接下來優化下操做系統的參數:
vi /etc/sysctl.conf
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default=262144
net.core.rmem_max=262144
net.core.wmem_default=262144
net.core.wmem_max=262144
net.ipv4.tcp_max_tw_buckets=6000
net.core.somaxconn=262144
net.core.netdev_max_backlog=262144
net.ipv4.tcp_max_syn_backlog=262144
fs.file-max = 6553560
net.ipv4.tcp_fin_timeout = 30
#net.ipv4.icmp_echo_ignore_all = 1 #這個是禁用被ping,防止被人ping掛了
sysctl -p #生效
給個網址:http://ifeve.com/inside-nginx-how-we-designed-for-performance-scale/