nginx的使用很是簡單,只須要配置好咱們須要的各類指令,就能跑起來。若是你須要添加模塊,還須要添加模塊方面的配置。通常在使用中,都是將nginx.conf拆分紅幾個小塊的文件,一邊管理。
javascript
一、nginx.conf配置css
#user nobody; #Linux存放線程的pid,每次啓動生成 #pid logs/nginx.pid; #和cpu核心同樣 worker_processes 1; #錯誤日誌 error_log logs/error.log error; #error_log logs/notice.log notice; #error_log logs/info.log info; #error_log logs/debug.log debug; #error_log logs/warn.log warn; events { worker_connections 1024; } http { include mime.types; #include 用戶加載另外的配置文件,例如其它配置內容過的 include gzip.conf; include proxy.conf; default_type application/octet-stream; server_tokens off; #關閉在錯誤頁面中的nginx版本數字 charset UTF-8; limit_conn addr 100; #每一個IP鏈接100個鏈接 root D:\website index index.html index.htm; # 命名爲main的日誌格式 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; #開啓文件從硬盤到網絡的傳輸,不須要經過緩存(減小IO,平緩硬盤和網絡的處理速度) tcp_nopush on; #此選項容許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用 #【響應設置,緩存和響應】 #在配置文件中設置自定義緩存以限制緩衝區溢出攻擊的可能性 client_max_body_size 50m; #請求體最大值 client_header_buffer_size 4k; #請求頭緩存最大值 client_body_buffer_size 256k; #請求體緩存最大值 large_client_header_buffers 8 128k; #客戶請求頭緩衝大小 client_header_timeout 3m; #請求頭超時 client_boddy_timeout 3m; #請求體超時 reset_timeout_connection #關閉不響應的客戶端鏈接。這將會釋放那個客戶端所佔有的內存空間 send_timeout 3m; #response的時候,超時時間 keepalive_timeout 65; #鏈接時間(秒) tcp_nodelay on; upstream tomcat{ ip_hash; server 192.168.217.1:8080 ; server 192.168.217.2:8080 ; } server{ listen 80; server_name tomcat.com; #動態代理 localhost =/{ proxy_redirect off; proxy_pass http://tomcat; } #錯誤 error_page 500 502 503 504 /50x.html; localhost =/50x.html{ } #靜態文件 localhost ~*.*\.(js|css)?${ expires 7d; #保存7天 access_log off; #關閉訪問日誌 } localhost ~*.*\(png|jpg|gif|jpeg|bmp|ico)?${ expires 7d; access_log off; } location ~* .*\.(zip|rar|exe|msi|iso|gho|mp3|rmvb|mp4|wma|wmv|rm)?$ { deny all; //禁止這些文件下載,你們能夠根據本身的環境來配置 } } }
二、proxy.conf配額制html
################ 設置傳送給後臺服務器的請求頭(主要是爲了session) ##### proxy_set_header Host $host; #表示客戶端請求頭部中的Host字段 proxy_set_header X-Real-IP $remote_addr; #客戶端IP地址 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #設置頭轉發 ######### proxy_request 設置 ################## proxy_connect_timeout 75s; #nginx跟後端服務器請求時間 proxy_rend_timeout 75s; #鏈接後,等候後端服務器響應時間處理時間。可能在鏈接表中 ########## proxy_response 設置 ################## proxy_send_timeout 75s; #鏈接成功後,後端服務器數據回傳時間_就是在規定時間以內後端服務器必須傳完全部的數據 proxy_buffer_size 64k; #代理服務器(nginx)保存用戶頭的緩衝區 proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k如下 proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件大小,大於這個值,將從後端服務器傳送,不用經過nginx緩存 proxy_ignore_client_abort on; #若是客戶端斷開請求,也保持與後端服務器的鏈接,防止服務器出現BUG
三、gzip.conf配置java
#【壓縮】 gzip on; gzip_disable "msie6"; #ie6一下不壓縮 #gzip_static on #告訴nginx在壓縮資源以前,先查找是否有預先gzip處理過的資源。這要求你預先壓縮你的文件(在這個例子中被註釋掉了),從而容許你使用最高壓縮比,這樣nginx就不用再壓縮這些文件了 gzip_proxied any; #容許或者禁止壓縮基於請求和響應的響應流 gzip_min_length 1000; #最小的壓縮文件,小於這個不壓縮 gzip_comp_level 4; #壓縮等級(1-9) gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; #壓縮類型