在主配置文件http段中添加一行include /data/nginx/conf.d/*.conf; 以引用/data/nginx/conf.d/目錄下所有以.conf結(jié)尾的文件
注:具體的目錄路徑以實際Nginx部署路徑?jīng)Q定
Nginx做web服務(wù)器
只單純的提供前端服務(wù)
在/data/nginx/conf.d/目錄下新建配置文件web.conf,文件內(nèi)容如下
server {
listen 8081;
server_name xxx.xxx.com;
location / {
root /data/web/dist; //前端靜態(tài)資源所在路徑
index index.html index.htm;
}
}
Nginx做文件服務(wù)器
在/data/nginx/conf.d/目錄下新建配置文件webfile.conf彩掐,文件內(nèi)容如下
server {
listen 8081;
server_name xxx.xxx.com;
location / {
root /data/web/file;
autoindex on; //開啟目錄瀏覽功能
charset 'utf-8'; //設(shè)置編碼格式為utf-8构舟,解決頁面顯示中文亂碼問題
}
}
Nginx做web服務(wù)器并代理后端接口
在/data/nginx/conf.d/目錄下新建配置文件web1.conf,文件內(nèi)容如下
server {
listen 8081;
server_name xxx.xxx.com;
root /data/web/dist;
index index.html index.htm;
location /api/ {
proxy_pass http://server.xxx.com;
}
}
Nginx location講解
location表達式類型
~ 表示執(zhí)行一個正則匹配堵幽,區(qū)分大小寫
~* 表示執(zhí)行一個正則匹配狗超,不區(qū)分大小寫
^~ 表示普通字符匹配。優(yōu)先使用前綴匹配朴下。如果匹配成功努咐,則不再匹配其他location。
= 進行普通字符精確匹配殴胧。也就是完全匹配渗稍。
location匹配優(yōu)先級
在nginx的location和配置中l(wèi)ocation的順序沒有太大關(guān)系。跟location表達式的類型有關(guān)团滥。相同類型的表達式竿屹,字符串長的會優(yōu)先匹配。
以下是按優(yōu)先級排列說明:
第一優(yōu)先級:等號類型(=)的優(yōu)先級最高灸姊。一旦匹配成功拱燃,則不再查找其他匹配項。
第二優(yōu)先級:^~類型表達式力惯。一旦匹配成功碗誉,則不再查找其他匹配項。
第三優(yōu)先級:正則表達式類型(~ ~*)的優(yōu)先級次之父晶。如果有多個location的正則能匹配的話哮缺,使用最先匹配的那個,即最上面的甲喝;
第四優(yōu)先級:常規(guī)字符串匹配類型蝴蜓。按前綴匹配。
nginx proxy_pass代理
proxy_pass 的url后跟"/"和不跟"/"的區(qū)別
# 第一種
location /abc {
proxy_pass http://192.168.200.129:8081/;
}
結(jié)論:會被代理到http://192.168.200.129:8081/index.html 這個url
# 第二種(相對于第一種俺猿,最后少一個 /)
location /abc {
proxy_pass http://192.168.200.129:8081;
}
結(jié)論:會被代理到http://192.168.200.129:8081/abc/index.html 這個url驗證
后端服務(wù)器地址為192.168.200.129:8081,后端服務(wù)器信息目錄結(jié)構(gòu)如下:
# tree dist/
dist/
├── abc
│ └── index.html
└── index.html
兩頁面內(nèi)容如下
# cat dist/index.html
<h1>hello word</h1>
# cat dist/abc/index.html
<h1>test abc</h1>
前端服務(wù)器地址為192.168.200.128:8088
第一種:
192.168.200.128:8088 nginx配置如下
server {
listen 8088;
server_name localhost;
location /abc/ {
proxy_pass http://192.168.200.129:8081/;
}
}
當我們訪問http://192.168.200.128:8088/abc/index.html 時候格仲,頁面出現(xiàn)的是hello word
第二種:
192.168.200.128:8088 nginx配置如下
server {
listen 8088;
server_name localhost;
location /abc/ {
proxy_pass http://192.168.200.129:8081;
}
}
當我們訪問http://192.168.200.128:8088/abc/index.html 時候押袍,頁面出現(xiàn)的是test abc
location 后面的路徑加"/"和不加"/"的區(qū)別
思考:如果我們指定 location 攔截特定的路徑時 location /test,test后面帶/和不帶/有什么區(qū)別呢
如果test后不帶"/"
表示會攔截 如/test /test01 /testxxxxx 這一類請求凯肋,只要是以test開頭的請求都會被攔截
如果test后帶"/"
表示只會攔截/test 或者/test/xxxx等相關(guān)請求
Nginx設(shè)置超時時間
1谊惭、在主配置文件http段中設(shè)置
有三個參數(shù)
client_header_timeout 12m;
指定等待client發(fā)送一個請求頭的超時時間(例如:GET / HTTP/1.1),僅當在一次read中,沒有收到請求頭圈盔,才會算成超時豹芯。如果在超時時間內(nèi),client沒發(fā)送任何東西驱敲,nginx返回HTTP狀態(tài)碼408(“Request timed out”)铁蹈,默認值 60s
client_body_timeout 12m;
該指令設(shè)置請求體(request body)的讀超時時間。僅當在一次readstep中众眨,沒有得到請求體握牧,就會設(shè)為超時。超時后娩梨,nginx返回HTTP狀態(tài)碼408(“Request timed out”)沿腰,默認值 60s
keepalive_timeout 720;
指定了與client的keep-alive連接超時時間,服務(wù)器將會在這個時間后關(guān)閉連接狈定。默認值 75s
示例如下
......
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;
sendfileon;
#tcp_nopush on;
#keepalive_timeout 0;
client_header_timeout 5m;
client_body_timeout 5m;
keepalive_timeout 300;
#gzip on;
include /data/nginx/conf.d/ *.conf;
server {
listen 80;
server_name localhost;
......
2颂龙、在server段location下設(shè)置
有三個參數(shù)
proxy_connect_timeout 720s;
后端服務(wù)器連接的超時時間,發(fā)起握手等候響應(yīng)超時時間(代理連接超時)纽什。默認值 60s
proxy_read_timeout 720s;
連接成功后措嵌,等候后端服務(wù)器響應(yīng)時間,其實已經(jīng)進入后端的排隊之中等候處理(也可以說是后端服務(wù)器處理請求的時間)稿湿。默認值 60s
proxy_send_timeout 720s;
后端服務(wù)器數(shù)據(jù)回傳時間_就是在規(guī)定時間之內(nèi)后端服務(wù)器必須傳完所有的數(shù)據(jù)铅匹。默認值 60s
示例如下
server {
listen 8081;
server_name xxx.xxx.com;
location / {
proxy_pass http://server.xxx.com;
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
}
Nginx上傳文件大小限制
nginx上傳文件大小限制取決于client_max_body_size參數(shù)
示例
client_max_body_size32M;
默認限制的文件大小是32M,如果想增加或者減少只需要修改后面的參數(shù)值即可
注意:client_max_body_size可以在http段下也可以在server段下饺藤,但是不能在location下
在http段下是作用于全局包斑,對所有server都生效,前提是server中沒有配置client_max_body_size,假如一個server中配置了client_max_body_size晶伦,那就會優(yōu)先使用自己server中的client_max_body_size配置蔗喂。
在server段下只針對單個server有效
Nginx解決跨域問題
server {
listen 80;
server_name xxx.xxx.com;
location /api/ {
proxy_pass http://server.xxx.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
或
server {
listen 80;
server_name xxx.xxx.com;
location /api/ {
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Headers *;
add_header Access-Control-Allow-Methods "GET, POST, PUT, OPTIONS";
proxy_passhttp://server.xxx.com;
}
}
Nginx Rewrite重寫
server {
listen 80;
server_name xxx.xxx.com;
location / {
root /data/web/zjtldt_app/dist;
index index.html index.htm;
}
location /t0 {
rewrite ^/t0(.*)$ $1 break; #通過rewrite將/t0路徑消除
proxy_passhttp://t0.tianditu.com;
}
}
此處的rewrite參數(shù)釋義如下
- ^:匹配輸入字符串的起始位置
- ():作為一個整體匹配
- .:匹配除換行符以外的任意字符
- *:匹配0次或多次
- $:匹配輸入字符串的結(jié)束位置
- $1:引用前面括號中(.*)匹配的內(nèi)容
- break:本條規(guī)則匹配完成即終止,不再匹配后面的任何規(guī)則
Nginx配置SSL證書
server {
listen 443 ssl;
server_name xxx.xxx.com;
ssl_certificate /data/nginx/ssl_file/xxx_xxx.xxx.pem; #證書文件
ssl_certificate_key /data/nginx/ssl_file/xxx_xxx.xxx.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;
location / {
root /data/dist;
index index.html index.htm;
}
location /api {
proxy_passhttp://192.168.200.20:8081/;
}
}
完整配置文件示例
server {
listen 443 ssl;
server_name xxx.xxx.com;
ssl_certificate /data/nginx/ssl_file/xxx_xxx.xin.pem;
ssl_certificate_key /data/nginx/ssl_file/xxx_xxx.xin.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;
location / {
root /data/web/zjtldt_app/dist;
index index.html index.htm;
}
location /api {
proxy_pass http://192.168.200.20:8081/;
}
location /file {
root /data/chatapp;
autoindex on;
charset 'utf-8';
}
location /dataserver {
proxy_pass http://192.168.200.20:48581/dataserver/;
}
location /AT_tileset {
proxy_pass http://192.168.200.20:18822/AT_tileset/;
}
location /t0 {
rewrite ^/t0(.*)$ $1 break;
proxy_pass http://t0.tianditu.com;
}
location /t1 {
rewrite ^/t1(.*)$ $1 break;
proxy_pass http://t1.tianditu.com;
}
location /t2 {
rewrite ^/t2(.*)$ $1 break;
proxy_passhttp://t2.tianditu.com;
}
location /t3 {
rewrite ^/t3(.*)$ $1 break;
proxy_passhttp://t3.tianditu.com;
}
location /t4 {
rewrite ^/t4(.*)$ $1 break;
proxy_passhttp://t4.tianditu.com;
}
location /t5 {
rewrite ^/t5(.*)$ $1 break;
proxy_passhttp://t5.tianditu.com;
}
location /t6 {
rewrite ^/t6(.*)$ $1 break;
proxy_passhttp://t6.tianditu.com;
}
location /t7 {
rewrite ^/t7(.*)$ $1 break;
proxy_passhttp://t7.tianditu.com;
}
}
說明
在該示例中萌抵,前端請求后臺接口地址或其他接口地址時不直接請求真實的接口地址,通過Nginx為接口地址配置反向代理元镀,前端請求時只請求Nginx的地址绍填,通過Nginx去訪問真實接口地址。
如為后臺接口地址http://192.168.200.2:8081/ 配置了nginx反向代理栖疑,前端請求后臺接口的地址改為Nginx地址https://xxx.xxx.com/api 讨永,此處的Nginx地址為已映射的外網(wǎng)地址。
這種模式的好處是真實接口地址不需要暴露給客戶端訪問遇革,只需要保證真實接口地址能被Nginx訪問卿闹,應(yīng)用發(fā)布時只需要暴露Nginx地址即可揭糕,對接口服務(wù)能起到保護作用,同時也不需要將接口地址映射到外網(wǎng)锻霎,減少NAT條數(shù)著角,減輕路由器壓力。
另一個好處是部署https應(yīng)用時旋恼,前端在Nginx上配置了https吏口,后臺接口服務(wù)如果不配置https的話訪問時會報錯“混合內(nèi)容”,需要為后臺接口服務(wù)也配置https蚌铜,才能解決該問題锨侯,除此種方式外,使用nginx為后臺接口配置反向代理冬殃,前端請求后臺接口時請求的是Nginx地址囚痴,因為Nginx已經(jīng)配置了https,可以有效解決混合內(nèi)容問題审葬。
注:公司部署的應(yīng)用全部使用該種模式深滚,只對外暴露Nginx訪問地址。
其他
對于并發(fā)訪問高的應(yīng)用來說涣觉,在server中設(shè)置不存儲訪問日志痴荐,可以讓讀取磁盤IO操作更快
示例如下
server {
listen 8081;
server_name xxx.xxx.com;
access_log off; #設(shè)置為off
location / {
root /data/web/zjtldt_app/dist;
index index.html index.htm;
}
}