nginx.conf
#user nobody; #配置用戶或者組,默認為nobody nobody worker_processes 4; #允許生成的進程數,默認為1 worker_cpu_affinity 00000001 00000010 00000100 00001000; #為每個進程分配一個CPU worker_rlimit_nofile 102400; #為nginx工作進程改變打開最多文件描述符數目的限制。用來在不重啟主進程的情況下增加限制。 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #指定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別依次為:debug|info|notice|warn|error|crit|alert|emerg #pid logs/nginx.pid; #指定nginx進程運行文件存放地址 events { accept_mutex on; #設置網路連接序列化,防止驚群現象發生,默認為on multi_accept on; #設置一個進程是否同時接受多個網絡連接,默認為off use epoll; #使用epoll(linux2.6的高性能方式)事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 102400; #最大連接數,默認為512 } http { include mime.types; #文件擴展名與文件類型映射表 default_type application/octet-stream; #默認文件類型,默認為text/plain lua_package_path "/usr/local/lib/lua/?.lua;;"; #lua庫位置 charset utf-8; #字符集 server_names_hash_bucket_size 128; # 保存服務器名字的hash表 client_header_buffer_size 4k; #用來緩存請求頭信息的,容量4K,如果header頭信息請求超過了且沒有配置client_header_buffer_size,nginx會直接返回400錯誤 large_client_header_buffers 4 32k; #如果large_buffer還是無法容納,那么就會返回414(處理request_line)/400(處理request_header)錯誤 client_max_body_size 300m; #允許客戶端請求的最大單文件字節數 tcp_nodelay on; #提高數據的實時響應性 client_body_buffer_size 512k; #緩沖區代理緩沖用戶端請求的最大字節數(請求多) proxy_connect_timeout 5s; #nginx跟后端服務器連接超時時間(代理連接超時) proxy_read_timeout 60s; #連接成功后,后端服務器響應時間(代理接收超時) proxy_send_timeout 5s; #后端服務器數據回傳時間(代理發送超時) proxy_buffer_size 16k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小 proxy_buffers 4 64k; #該指令設置緩沖區的大小和數量,從被代理的后端服務器取得的響應內容,會放置到這里 proxy_busy_buffers_size 128k; #所有處在busy狀態的buffer size加起來不能超過proxy_busy_buffers_size proxy_temp_file_write_size 128k; #如果response的內容很大的話,Nginx會接收並把他們寫入到temp_file里去。busy的buffer傳輸完了會從temp_file里面接着讀數據,直到傳輸完畢 gzip on; #NGINX可以壓縮靜態資源 gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; #壓縮級別大小,最小1,最大9,值越小,壓縮后比例越小,CPU處理更快; 值越大壓縮后占用帶寬越少。 gzip_types text/plain application/x-javascript text/css application/xml; #壓縮類型:text js css xml 都會被壓縮 gzip_vary on; #作用是在http響應中增加一行,目的是改變反向代理服務器的緩存策略 #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; #access_log off; #取消服務日志 #日志格式 # ip 遠程用戶 當地時間 請求URL 狀態 發送的大小 響應的頭 客戶端使用的瀏覽器 頁面響應的時間 log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $request_time $http_x_forwarded_for'; #自定義格式 access_log logs/access.log myFormat; #combined為日志格式的默認值 sendfile on; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊 #sendfile_max_chunk 100k; #每個進程每次調用傳輸數量不能大於設定的值,默認為0,即不設上限 #tcp_nopush on; tcp_nopush on; #防止網絡阻塞 #keepalive_timeout 0; keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊 #gzip on; #上游服務器 upstream mysvr { #負載均衡算法,默認為round-robin輪循 ip_hash; #每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題,ip_hash不支持weight和backup server 192.168.5.91:7878 max_fails=2 fail_timeout=10s; server 192.168.5.92:7878 max_fails=2 fail_timeout=10s; #server 192.168.5.91:7878 max_fails=2 fail_timeout=10s weight=1; #server 192.168.5.92:7878 max_fails=2 fail_timeout=10s weight=2; #server 192.168.5.90:7878 backup; #熱備 } #error_page 404 https://www.baidu.com; #錯誤頁 server { keepalive_requests 120; #單連接請求上限次數 listen 9080; #監聽端口 server_name localhost; #監聽地址 127.0.0.1 #charset koi8-r; #access_log logs/host.access.log main; #location ~*^.+$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。 # #root path; #根目錄 # #index vv.txt; #設置默認頁 # proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器列表 # deny 127.0.0.1; #拒絕的ip # allow 172.18.5.54; #允許的ip #} location /test { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_next_upstream_timeout 10s; proxy_next_upstream_tries 2; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header upstream_addr $upstream_addr; proxy_pass http://mysvr; } #nginx主頁 location / { root html; index index.html index.htm; } #用lua腳本向reids存值 location /lua/set { default_type 'text/plain'; content_by_lua_file conf/lua/setKeyValue.lua; } #用lua腳本從reids取值 location /lua/get { default_type 'text/plain'; content_by_lua_file conf/lua/getKey.lua; } #靜態資源代理 location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /var/local/static; expires 30d; } #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; } }