組態檔的位置:/usr/local/nginx/conf/nginx.conf
user
#user www www;
nginx 運行的用戶和用戶組,默認為 nobody
pid
pid logs/nginx.pid;
行程檔案配置,默認為 logs/nginx.pid
error_log
error_log logs/error.log warn;
錯誤日志配置,
日志級別可選:debug < info < notice < warn < error < crit
日志級別越高,記錄的日志資訊越少
worker_processes
worker_processes 1;
nginx 對外提供 web 服務的 worder 行程數,通常設定和 cpu 核數的數量相等
worker_rlimit_nofile
worker_rlimit_nofile 1024;
一個 worker 行程能打開的檔案的最大數目,
理論值應該是系統最多能打開的檔案數(在 linux 中執行 ulimit -n 可得到值)與 worker 行程數相
除得到的值,但是 nginx 分配請求并不均勻,建議與 ulimit -n 的值保持一致
events
events {
worker_connections 1024;
use epoll;
}
worker_connections
worker_connections 1024;
設定單個 worker 行程最大連接數
use
use epoll;
設定復用客戶端執行緒的輪詢方法
use [ kqueue | rtsig | epoll | select | poll ]
epoll 是 linux 2.6 以上版本內核中的高性能網路 I / O 模型
如果不設定,nginx 會選擇一個最適合你作業系統的模型
http
http {
}
http 服務器配置
include
include mime.types;
設定 MIME 型別(資源的媒體型別),通過 http 協議由 web 服務器回傳給客戶端瀏覽器,瀏覽器會自動使用指定應用程式來打開資源檔案
mime.types 檔案位于 /usr/local/nginx/conf/mime.types
default_type
default_type application/octet-stream;
默認的資源檔案的媒體型別,application/octet-stream 為任意的二進制資料流傳輸
log_format
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
日志格式,"log_format main" 這里的 main 是日志格式的名稱,通過不同名稱來定義多種日志格式

access_log
access_log logs/access.log main;
配置訪問日志,可以指定使用的日志格式,如這里的 main
charset
charset UTF-8;
使用的字符集編碼
sendfile
sendfile on;
通過呼叫 sendfile 函式(sendfile 是一個系統呼叫,在內核空間中完成),可以高效的發送靜態檔案
tcp_nopush
tcp_nopush on;
一次性發送資料包,與 tcp_nodelay 配置項互斥,與 sendfile 配合作業
tcp_nodelay
#tcp_nodelay on;
每 0.2 秒后立即發送一個資料包,可以有效的防止網路阻塞,當需要及時發送資料時,應該開啟它
keepalive_timeout
keepalive_timeout 60;
配置長連接的超時時間,服務器將在這個超時時間過后關閉鏈接
client_header_timeout
client_header_timeout 120;
等待客戶端發送一個請求頭的超時時間,超時 Nginx 回傳一個請求超時的狀態碼(408,Request Timeout)
client_body_timeout
client_body_timeout 120;
等待客戶端發送一個請求體的超時時間,超時 Nginx 回傳一個請求超時的狀態碼(408,Request Timeout)
send_timeout
send_timeout 120;
客戶端讀取資料的超時時間,超時 Nginx 就會關閉該連接
client_header_buffer_size
client_header_buffer_size 16k;
快取客戶端請求頭的大小
large_client_header_buffers
large_client_header_buffers 4 32k;
快取客戶端請求頭的最大大小,此處為 4 個 32K 的大小
gzip
gzip on;
采用 gzip 壓縮的形式發送資料,可以減少發送的資料量
gzip_min_length
gzip_min_length 1k;
大于該值的內容才壓縮,0 表示所有的內容都壓縮
gzip_buffers
gzip_buffers 4 16k;
快取 gzip 壓縮資料的空間大小,此處為 4 個 16K 的大小
gzip_types
gzip_types text/plain text/css application/x-javascript application/xml;
只有匹配配置的 MIME 型別的檔案內容才會被壓縮
nginx 默認會對 text/html 型別進行壓縮,此處無需再配,若配置 text/html,nginx 會發出警告:
nginx: [ warn ] duplicate MIME type "text/html"
server_names_hash_bucket_size
server_names_hash_bucket_size 128;
服務器名稱的 hash 表大小,如果太小,Nginx 無法啟動
server
server {
}
虛擬主機配置
listen
listen 80;
監聽埠
server_name
server_name fanlychie.com www.fanlychie.com;
域名,可以配置多個,以空格分開
access_log
access_log logs/fanlychie.access.log main;
虛擬主機訪問日志,
root
root www.fanlychie.com;
網站根目錄,用于存放網站靜態資源檔案,若為相對路徑,則是相對于 nginx 安裝的目錄,如此處的物理路徑為 /usr/local/nginx/www.fanlychie.com
location
location / {
}

多個 location 匹配 url 地址的優先級:
"=" 被匹配,停止搜索其他匹配
"^~" 被匹配,停止搜索其他匹配
"~" 和 "~*" 按組態檔中出現的先后順序,一旦匹配,停止搜索其他匹配
若以上都沒有匹配到,則交給 "/" 處理請求
expires
location ~ \.(js|css)$ {
expires 1d;
}
js 和 css 檔案由 nginx 直接提供,快取一天
如客戶端訪問 http://www.fanlychie.com/styles/main.css
由于上面已經配置 root www.fanlychie.com,nginx 會將
/usr/local/nginx/www.fanlychie.com/styles/main.css 檔案直接回傳給客戶端
location ~ \.(gif|jpg|jpeg|png|ico)$ {
expires 7d;
}
同上面,圖片檔案快取 7 天
deny
location ~* \.jsp {
deny all;
}
禁止直接訪問 *.jsp 檔案(回傳 403 狀態碼給客戶端)
proxy_pass
location = / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www.fanlychie.com/index;
}
proxy_pass 配置反向代理
即通過域名訪問網站首頁,nginx 將請求轉發給真實的運用服務器處理,即
http://www.fanlychie.com/index (www.fanlychie.com 是上面配置的 server_name 的名稱)
proxy_set_header

location / {
proxy_pass http://www.fanlychie.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
}
proxy_redirect
proxy_redirect 與 proxy_pass 用法類似,proxy_pass 是轉發請求,客戶端 url 地址不變;
proxy_redirect 是請求重定向,客戶端 url 地址發生變化,
client_max_body_size
允許客戶端請求的最大單檔案位元組數
client_body_buffer_size
代理緩沖用戶端請求的緩沖區最大位元組數
proxy_connect_timeout
代理連接超時時間(nginx 跟后端服務器連接超時時間)
proxy_send_timeout
代理發送超時時間(后端服務器資料回傳時間)
proxy_read_timeout
代理接收超時時間(連接成功后,后端服務器回應時間)
proxy_buffers
代理緩沖區的大小
error_page
location = /403.html {
root html;
}
location = /404.html {
root html;
}
location = /50x.html {
root html;
}
error_page 403 /403.html;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
錯誤頁面配置,
upstream
upstream www.fanlychie.com {
server 192.168.1.102:8080;
server 192.168.1.103:8080;
}
負載均衡配置
upstream www.fanlychie.com,此處的 www.fanlychie.com 是上面 server 模塊配置的
server_name,server 指定真實的運用服務主機地址,可以配置多個,
nginx 默認采用輪詢的策略,將請求平均的分配到各個運用服務主機中,當其中一臺服務主機宕機
后,會自動的被剔除,另外一臺服務主機仍然能正常提供服務,
weight
upstream www.fanlychie.com {
server 192.168.1.102:8080 weight=1;
server 192.168.1.103:8080 weight=2;
};
在輪詢的基礎上加上權重,數值越大,表示權值越大,nginx 分發的請求越多
用于運用服務器性能不均的情況,當其中一臺服務主機宕機后,會自動的被剔除,另外一臺服務主
機仍然能正常提供服務
ip_hash
upstream www.fanlychie.com {
ip_hash;
server 192.168.1.102:8080;
server 192.168.1.103:8080;
}
每個請求按訪問 ip 的 hash 結果分配服務主機
當新的請求到達時,先將用戶 ip 通過哈希演算法求值,在隨后的請求客戶端 ip 的哈希值只要相同,
就會被分配至同一個后端服務器,這種調度可以解決 session 共享的問題,但有時會導致分配不均
即無法保證負載均衡
#user nobody;
pid logs/nginx.pid;
worker_processes 1;
worker_rlimit_nofile 1024;
error_log logs/error.log warn;
events {
worker_connections 1024;
use epoll;
}
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;
charset UTF-8;
sendfile on;
tcp_nopush on;
#tcp_nodelay on;
keepalive_timeout 60;
client_header_timeout 120;
client_body_timeout 120;
send_timeout 120;
client_header_buffer_size 16k;
large_client_header_buffers 4 32k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_types text/plain text/css application/x-javascript application/xml;
server_names_hash_bucket_size 128;
upstream www.fanlychie.com {
server 192.168.1.102:8080;
server 192.168.1.103:8080;
}
server {
listen 80;
server_name fanlychie.com www.fanlychie.com;
access_log logs/fanlychie.access.log main;
root www.fanlychie.com;
location = / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://www.fanlychie.com/index;
}
location ~ \.(js|css)$ {
expires 1d;
}
location ~ \.(gif|jpg|jpeg|png|ico)$ {
expires 7d;
}
location ~* \.jsp {
deny all;
}
location / {
proxy_pass http://www.fanlychie.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
}
location = /403.html {
root html;
}
location = /404.html {
root html;
}
location = /50x.html {
root html;
}
error_page 404 /404.html;
error_page 403 /403.html;
error_page 500 502 503 504 /50x.html;
}
}
附全域變數表
| 變數 | 描述 |
|---|---|
| $args | 請求的引數 |
| $content_length | 請求頭中的 Content-length 域的值 |
| $content_type | 請求頭中的 Content-Type 域的值 |
| $host | 請求主機域的值,否則為服務器名稱 |
| $http_user_agent | 客戶端 agent 資訊 |
| $http_cookie | 客戶端 cookie 資訊 |
| $request_method | 客戶端請求的動作,通常為 GET 或 POST |
| $remote_addr | 客戶端的 IP 地址 |
| $remote_port | 客戶端的埠 |
| $remote_user | 已經經過 Auth Basic Module 驗證的用戶名 |
| $request_filename | 當前請求的檔案路徑 |
| $query_string | 與 $args 同 |
| $scheme HTTP | 方法(如 http,https) |
| $server_protocol | 請求使用的協議,通常是 HTTP/1.0 或 HTTP/1.1 |
| $server_addr | 服務器地址,在完成一次系統呼叫后可以確定這個值 |
| $server_name | 服務器名稱 |
| $server_port | 請求到達服務器的埠號 |
| $request_uri | 包含請求引數的原始 URI,不包含主機名,如:"/foo/bar.php?arg=baz" |
| $uri | 不帶請求引數的當前 URI,$uri 不包含主機名,如 “/foo/bar.html” |
| $document_uri | 與 $uri 同 |
附運算式串列
| 運算式 | 運算式 | 描述 |
|---|---|---|
| -f | !-f | 判斷檔案是否存在 |
| -d | !-d | 判斷目錄是否存在 |
| -e | !-e | 判斷檔案或目錄是否存在 |
| -x | !-x | 判斷檔案是否可執行 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/348336.html
標籤:其他
下一篇:我的畢設實戰指南
