1. 程式人生 > 其它 >13 Nginx 配置優化與反向代理兩個tomcat

13 Nginx 配置優化與反向代理兩個tomcat

文件:http://nginx.org/en/docs/
ngin_status: 併發統計
Ngxtop : 請求統計

1 併發優化

sudo vim /usr/local/nginx/conf/nginx.conf

user gigabyte;  #使用者
worker_processes  4; #cpu
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


pid        logs/nginx.pid;
# 併發相關的引數
worker_rlimit_nofile 20480; # 每個程序開啟的最大的檔案數,受限於作業系統
events {
    use epoll; 
    multi_accept on;  # 可以一次建立多個連線
    worker_connections  10240; # 單個工作程序最大併發連線數
}

2 KeepAlive長連線

# 長連線
keepalive_timeout  65;# 長連線的超時時間
keepalive_requests 500;# 500個請求之後就關閉連線,可以調大
keepalive_disable msie6; # ie6 禁用

3 壓縮優化

gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x-javascript application/json application/xml application/vnd.ms-fontobject application/x-font-ttf application/svg+xml application/x-icon;
gzip_vary on; #Vary: Accept-Encoding
gzip_static on; # 如果有壓縮好的,直接使用


如果遇到報錯不要慌,根據提示去進行就好,這裡提示使用--without-http_gzip_module,那就用唄,解決~

4 配置快取

1 安裝

  • 將上面幾個安裝包放到/usr/local/src目錄下然後解壓
cd nginx-1.15.9
sudo ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.38 --with-http_stub_status_module --with-http_gzip_static_module --add-module=/usr/local/src/ngx_cache_purge-2.3
sudo make
sudo make install
# 啟動nginx
cd /usr/local/nginx/sbin
sudo ./nginx
sudo ./nginx -s stop
sudo ./nginx -s reload

2 配置

sudo vim /usr/local/nginx/conf/nginx.conf

#1 併發優化
user gigabyte;  #使用者
worker_processes  4; #cpu
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


pid        logs/nginx.pid;
# 併發相關的引數
worker_rlimit_nofile 20480; # 每個程序開啟的最大的檔案數,受限於作業系統
events {
    use epoll; 
    multi_accept on;  # 可以一次建立多個連線
    worker_connections  10240; # 單個工作程序最大併發連線數
}




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;
    # 預設寫日誌:開啟檔案寫入關閉,max:快取的檔案描述符數量,inactive快取時間,
    # valid:檢查時間間隔,
    # min_uses: 在inactive時間段內使用了多詩詞加入快取
    open_log_file_cache max=200 inactive=20s valid=1m min_uses=2;


    sendfile        on;
    tcp_nopush     on;


    #keepalive_timeout  0;
    #keepalive_timeout  65;

    # 2 長連線
    keepalive_timeout  65;# 長連線的超時時間
    keepalive_requests 500;# 500個請求之後就關閉連線,可以調大
    keepalive_disable msie6; # ie6 禁用


    #gzip  on;
    # 3 壓縮相關
    gzip on;
    gzip_http_version 1.1;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)"; # 在IE 1-6禁用
    gzip_proxied any; # 無論在那個代理過來的都啟用
    gzip_types text/plain text/css application/javascript application/x-javascript application/json application/xml application/vnd.ms-fontobject application/x-font-ttf application/svg+xml application/x-icon;
    gzip_vary on; #Vary: Accept-Encoding
    gzip_static on; # 如果有壓縮好的,直接使用
    # 超時時間
    proxy_connect_timeout 5; # 連線proxy超時
    proxy_send_timeout 5; # proxy連線nginx超時
    proxy_read_timeout 60;# proxy響應超時


    # 反向代理伺服器叢集
    upstream server_pool{ 
        server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
        server localhost:8081 weight=1 max_fails=2 fail_timeout=30s; 
        keepalive 200; # 最大的空閒的長連線數
    }
    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            # root   html;
            # index  index.html index.htm; 
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade; # 與http 2.0 相關
            proxy_set_header Connection "upgrade"; # 與http 2.0 相關
            #Tomcat獲取真實使用者ip
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto  $scheme;
            proxy_pass http://server_pool;
        }
        # 狀態監控
        location /nginx_status {
            stub_status on;
            access_log   off;
            allow 127.0.0.1;
            allow 10.23.23.25;
            allow 10.23.23.33;
            deny all;
        }


        #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;
    #    }
    #}
}
  • NOTE1 : worker_rlimit_nofile 204800
  • 如果設定了worker_connections=204800, 那麼需要同時修改/etc/security/limits.conf
    vi /etc/security/limits.conf
  • hard nofile 204800
  • soft nofile 204800
  • soft core unlimited
  • soft stack 204800

    修改完之後重啟
    limit -n
  • NOTE2: 如果開啟了反向代理,那麼就
    開啟了 ----> upstream server_pool{
    server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
    server localhost:8081 weight=1 max_fails=2 fail_timeout=30s;
    keepalive 200; # 最大的空閒的長連線數
    }
    那麼要在location中設定
    location / {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    }
  • NOTE: 配置檔案修改完之後,檢查語法是否正確
    ./sbin/nginx -t

    然後重啟
    cd /usr/local/nginx
    sudo ./sbin/nginx -s reload

3 根據埠號檢視程序

sudo netstat -nap | grep 808

如果你想用nginx管理兩個tomcat ,那麼你需要有兩個tomcat才行哦


目前只成功了一個。