概述
安裝與使用
安裝
從源代碼編譯 Nginx
Windows 安裝
使用
nginx 配置實戰
http反向代理配置
負載均衡配置
網站有多個webapp的配置
https反向代理配置
參考
概述
什么是nginx?
Nginx (engine x) 是一款輕量級的Web 服務器 、反向代理服務器及電子郵件(IMAP/POP3)代理服務器。
什么是反向代理?
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,并將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個反向代理服務器。
可參考下圖的示例:
安裝與使用
安裝
nginx官網下載地址
發布版本分為Linux和windows版本。
也可以下載源碼,編譯后運行。
從源代碼編譯 Nginx
把源碼解壓縮之后,在終端里運行如下命令:
1
2
3
|
./configure make sudo make install |
默認情況下,Nginx 會被安裝在 /usr/local/nginx。通過設定編譯選項,你可以改變這個設定。
Windows 安裝
為了安裝Nginx/Win32,需先下載它。然后解壓之,然后運行即可。下面以C盤根目錄為例說明下:
1
2
|
cd C: cd C:\nginx-0.8.54 start nginx |
Nginx/Win32是運行在一個控制臺程序,而非windows服務方式的。服務器方式目前還是開發嘗試中。
使用
nginx的使用比較簡單,就是幾條命令。
常用到的命令如下:
nginx -s stop 快速關閉Nginx,可能不保存相關信息,并迅速終止web服務。
nginx -s quit 平穩關閉Nginx,保存相關信息,有安排的結束web服務。
nginx -s reload 因改變了Nginx相關配置,需要重新加載配置而重載。
nginx -s reopen 重新打開日志文件。
nginx -c filename 為 Nginx 指定一個配置文件,來代替缺省的。
nginx -t 不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,并嘗試打開配置文件中所引用到的文件。
nginx -v 顯示 nginx 的版本。
nginx -V 顯示 nginx 的版本,編譯器版本和配置參數。
如果不想每次都敲命令,可以在nginx安裝目錄下新添一個啟動批處理文件startup.bat,雙擊即可運行。內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@ echo off rem 如果啟動前已經啟動nginx并記錄下pid文件,會 kill 指定進程 nginx.exe -s stop rem 測試配置文件語法正確性 nginx.exe -t -c conf/nginx.conf rem 顯示版本信息 nginx.exe -v rem 按照指定配置去啟動nginx nginx.exe -c conf/nginx.conf |
nginx 配置實戰
我始終認為,各種開發工具的配置還是結合實戰來講述,會讓人更易理解。
http反向代理配置
我們先實現一個小目標:不考慮復雜的配置,僅僅是完成一個http反向代理。
nginx.conf配置文件如下:
注:conf/nginx.conf是nginx的默認配置文件。你也可以使用nginx -c指定你的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#運行用戶 #user somebody; #啟動進程,通常設置成和cpu的數量相等 worker_processes 1; #全局錯誤日志 error_log D:/Tools/nginx-1.10.1/logs/error.log; error_log D:/Tools/nginx-1.10.1/logs/notice.log notice; error_log D:/Tools/nginx-1.10.1/logs/info.log info; #PID文件,記錄當前啟動的nginx的進程ID pid D:/Tools/nginx-1.10.1/logs/nginx.pid; #工作模式及連接數上限 events { worker_connections 1024; #單個后臺worker process進程的最大并發鏈接數 } #設定http服務器,利用它的反向代理功能提供負載均衡支持 http { #設定mime類型(郵件支持類型),類型由mime.types文件定義 include D:/Tools/nginx-1.10.1/conf/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 D:/Tools/nginx-1.10.1/logs/access.log main; rewrite_log on; #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對于普通應用, #必須設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime. sendfile on; #tcp_nopush on; #連接超時時間 keepalive_timeout 120; tcp_nodelay on; #gzip壓縮開關 #gzip on; #設定實際的服務器列表 upstream zp_server1{ server 127.0.0.1:8089; } #HTTP服務器 server { #監聽80端口,80端口是知名端口號,用于HTTP協議 listen 80; #定義使用www.xx.com訪問 server_name www.helloworld.com; #首頁 index index.html #指向webapp的目錄 root D:\01_Workspace\Project\github\zp\SpringNotes\spring -security \spring -shiro \src\main\webapp; #編碼格式 charset utf-8; #代理配置參數 proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; proxy_set_header Host $host ; proxy_set_header X -Forwarder -For $remote_addr; #反向代理的路徑(和upstream綁定),location 后面設置映射的路徑 location / { proxy_pass http://zp_server1; } #靜態文件,nginx自己處理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { root D:\01_Workspace\Project\github\zp\SpringNotes\spring -security \spring -shiro \src\main\webapp\views; #過期30天,靜態文件不怎么更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。 expires 30d; } #設定查看Nginx狀態的地址 location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus" ; auth_basic_user_file conf/htpasswd; } #禁止訪問 .htxxx 文件 location ~ /\.ht { deny all; } #錯誤處理頁面(可選擇性配置) #error_page 404 /404.html; #error_page 500 502 503 504 /50x.html; #location = /50x.html { # root html; #} } } |
好了,讓我們來試試吧:
1.啟動webapp,注意啟動綁定的端口要和nginx中的upstream設置的端口保持一致。
2.更改host:在C:\Windows\System32\drivers\etc目錄下的host文件中添加一條DNS記錄
127.0.0.1 www.helloworld.com
3.啟動前文中startup.bat的命令
4.在瀏覽器中訪問www.helloworld.com,不出意外,已經可以訪問了。
負載均衡配置
上一個例子中,代理僅僅指向一個服務器。
但是,網站在實際運營過程中,多半都是有多臺服務器運行著同樣的app,這時需要使用負載均衡來分流。
nginx也可以實現簡單的負載均衡功能。
假設這樣一個應用場景:將應用部署在192.168.1.11:80、192.168.1.12:80、192.168.1.13:80三臺linux環境的服務器上。網站域名叫www.helloworld.com,公網IP為192.168.1.11。在公網IP所在的服務器上部署nginx,對所有請求做負載均衡處理。
nginx.conf配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
http { #設定mime類型,類型由mime.type文件定義 include /etc/nginx/mime.types; default_type application/octet-stream; #設定日志格式 access_log / var /log/nginx/access.log; #設定負載均衡的服務器列表 upstream load_balance_server { #weigth參數表示權值,權值越高被分配到的幾率越大 server 192.168.1.11:80 weight=5; server 192.168.1.12:80 weight=1; server 192.168.1.13:80 weight=6; } #HTTP服務器 server { #偵聽80端口 listen 80; #定義使用www.xx.com訪問 server_name www.helloworld.com; #對所有請求進行負載均衡請求 location / { root /root; #定義服務器的默認網站根目錄位置 index index.html index.htm; #定義首頁索引文件的名稱 proxy_pass http: //load_balance_server ;#請求轉向load_balance_server 定義的服務器列表 #以下是一些反向代理的配置(可選擇性配置) #proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 90; #nginx跟后端服務器連接超時時間(代理連接超時) proxy_send_timeout 90; #后端服務器數據回傳時間(代理發送超時) proxy_read_timeout 90; #連接成功后,后端服務器響應時間(代理接收超時) proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小 proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置 proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大于這個值,將從upstream服務器傳 client_max_body_size 10m; #允許客戶端請求的最大單文件字節數 client_body_buffer_size 128k; #緩沖區代理緩沖用戶端請求的最大字節數 } } } |
網站有多個webapp的配置
當一個網站功能越來越豐富時,往往需要將一些功能相對獨立的模塊剝離出來,獨立維護。這樣的話,通常,會有多個webapp。
舉個例子:假如www.helloworld.com站點有好幾個webapp,finance(金融)、product(產品)、admin(用戶中心)。訪問這些應用的方式通過上下文(context)來進行區分:
www.helloworld.com/finance/
www.helloworld.com/product/
www.helloworld.com/admin/
我們知道,http的默認端口號是80,如果在一臺服務器上同時啟動這3個webapp應用,都用80端口,肯定是不成的。所以,這三個應用需要分別綁定不同的端口號。
那么,問題來了,用戶在實際訪問www.helloworld.com站點時,訪問不同webapp,總不會還帶著對應的端口號去訪問吧。所以,你再次需要用到反向代理來做處理。
配置也不難,來看看怎么做吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
http { #此處省略一些基本配置 upstream product_server{ server www.helloworld.com:8081; } upstream admin_server{ server www.helloworld.com:8082; } upstream finance_server{ server www.helloworld.com:8083; } server { #此處省略一些基本配置 #默認指向product的server location / { proxy_pass http: //product_server; } location /product/{ proxy_pass http: //product_server; } location /admin/ { proxy_pass http: //admin_server; } location /finance/ { proxy_pass http: //finance_server; } } } |
https反向代理配置
一些對安全性要求比較高的站點,可能會使用HTTPS(一種使用ssl通信標準的安全HTTP協議)。
這里不科普HTTP協議和SSL標準。但是,使用nginx配置https需要知道幾點:
HTTPS的固定端口號是443,不同于HTTP的80端口
SSL標準需要引入安全證書,所以在nginx.conf中你需要指定證書和它對應的key
其他和http反向代理基本一樣,只是在Server部分配置有些不同。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#HTTP服務器 server { #監聽443端口。443為知名端口號,主要用于HTTPS協議 listen 443 ssl; #定義使用www.xx.com訪問 server_name www.helloworld.com; #ssl證書文件位置(常見證書文件格式為:crt/pem) ssl_certificate cert.pem; #ssl證書key位置 ssl_certificate_key cert.key; #ssl配置參數(選擇性配置) ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; #數字簽名,此處使用MD5 ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /root; index index.html index.htm; } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!