我整理了一份Nginx的配置文件說明,是真正經歷過正式線上考驗過。若是有優化的地方,也請朋友們指點一二,整理出一份比較全而實用的配置。javascript
主要包含配置:負載均衡配置,頁面重定向,轉發,HTTPS和HTTP的配置, 緩存優化,錯誤頁面配置等。php
#user nobody; #工做進程,於CPU核數一致 worker_processes 2; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { # 設置可鏈接數 worker_connections 2048; #優化同一時刻只有一個請求而避免多個睡眠進程被喚醒的設置,on爲防止被同時喚醒,默認爲off,所以nginx剛安裝完之後要進行適當的優化。 accept_mutex on; #打開同時接受多個新網絡鏈接請求的功能 multi_accept on; } http { include mime.types; default_type application/octet-stream; #隱藏nginx的版本號 server_tokens off; #日誌格式 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; #保持鏈接超時時間 keepalive_timeout 60; # 啓動內容壓縮,有效下降網絡流量 gzip on; # 太短的內容壓縮效果不佳,壓縮過程還會浪費系統資源 gzip_min_length 1000; # 可選值1~9,壓縮級別越高壓縮率越高,但對系統性能要求越高 gzip_comp_level 4; # 壓縮的內容類別 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; # 靜態文件緩存 open_file_cache max=65535 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; #server { # listen 80; # server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # location / { # 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; # } #} #負載均衡 使用ip_hash策略,集羣部署 upstream raysonblog.cn { server 123.45.678.901:8081 weight=10 max_fails=1 fail_timeout=10s; server 123.45.678.902:8089 weight=5 max_fails=1 fail_timeout=10s; } # HTTPS server server { listen 443 ssl; server_name www.raysonblog.cn; # 開啓 ssl ssl on; # 指定 ssl 證書路徑 ssl_certificate cert/www.raysonblog.cn_bundle.crt; # 指定私鑰文件路徑 ssl_certificate_key cert/raysonblog.cn.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; # 支持Websocket proxy_set_header Connection "upgrade"; # 支持Websocket proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; # 真實用戶訪問協議 proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_max_temp_file_size 0; # 配置緩存 client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 18000; proxy_send_timeout 18000; proxy_read_timeout 18000; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; # 處理/項目名a/a.jsp重定向到/項目名b/b.jsp location = /項目名a/a.jsp { rewrite . /項目名b/b.jsp permanent; # permanent 永久 } location /項目名c { # 轉發 proxy_pass http://127.0.0.1:8080/項目名c; } location /項目名d { #轉發 proxy_pass http://127.0.0.1:8080/項目名d; } location /blog { # 負載均衡跟upstream 的配置一致 proxy_pass http://raysonblog.cn/blog; } } # HTTP Server server {# 服務名及配置,一個服務下能夠有多個location用來表示不一樣的反向代理 listen 80; # 端口號 server_name www.raysonblog.cn; #主機名,默認是本主機 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; # 真實用戶訪問協議 proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_max_temp_file_size 0; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 18000; proxy_send_timeout 18000; proxy_read_timeout 18000; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; location = /MP_verify_1wqwq.txt { root weixin; #weixin是nginx目錄下html文件裏的weixin文件夾 } # 處理/項目名a/a.jsp重定向到/項目名b/b.jsp location = /項目名a/a.jsp { rewrite . /項目名b/b.jsp permanent; # permanent 永久 } location /項目名c { # 轉發 proxy_pass http://127.0.0.1:8080/項目名c; } location /項目名d { #轉發 proxy_pass http://127.0.0.1:8080/項目名d; } location /blog { # 負載均衡跟upstream 的配置一致 proxy_pass http://raysonblog.cn/blog; } } }
後續也會繼續完善此配置,你們在配置的時候,基本上能夠知足應用了。。。 微信關注:「Java技術乾貨」,或掃下方二維碼。即刻關注,不迷路。 css
原文出處:https://www.cnblogs.com/werewolfBoy/p/11066021.htmlhtml