文件目錄
test
│
├─test0
│ index.html
├─test1
│ index.html
└─test2
│ index.html
├─test2-1
│ index.html
└─test2-2
index.html
代理前端
代理單個前端時,以下eg1、eg2代理的是同一個文件薪棒,不用的是url
- localhost:8080/
- localhost:8080/test0
server {
listen 8080;
server_name localhost;
# eg1
location / {
root /root/test/test0;
index index.html index.htm;
}
# eg2
location /test0 {
root /root/test;
index index.html index.htm;
}
# eg3
location /test1 {
alias /root/test/test1/;
index index.html index.htm;
}
}
細心地讀者發(fā)現(xiàn)還有第三個代理eg3累奈、它的不同在于19行餐蔬,是以alias開頭的代理。那么他有什么不同呢恕稠,按照上面代理文件的路徑琅绅,test1與test0是一樣的,也就是說eg1和eg3是一樣的代理鹅巍。
location | root/alias | 文件實際路徑 | |
---|---|---|---|
eg1 | / | /root/test/test0 | /root/test/test0/index.html |
eg2 | /test0 | /root/test | /root/test/test0/index.html |
eg3 | /test1 | /root/test/test1/ | /root/test1/test1/index.html |
簡單的分析出:
root:root + location 為實際文件路徑
alias:alias 為實際文件路徑(ps:必須是以/結尾奉件,因為代理的東西在此目錄下)
代理多個前端
當代理多個靜態(tài)文件時,容易發(fā)生404問題昆著。
若把單個配置成location /
時县貌,又沒有問題,那么你需要參考以下配置凑懂。
ps:一般根路徑用root煤痕,其他為alias代理。
server {
listen 8080;
server_name localhost;
location / {
root /root/test/test0;
index index.html index.htm;
}
location /test1 {
alias /root/test/test1/;
index index.html index.htm;
}
location /test2 {
alias /root/test/test2/;
index index.html index.htm;
}
location /test2-1 {
alias /root/test/test2/test2-1/;
index index.html index.htm;
}
location /test2-2 {
alias /root/test/test2/test2-2/;
index index.html index.htm;
}
}
代理前端和后端
有關^~
請看FAQ
12行最后的/api/;
分號前面的/
問題請看FAQ
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html; # 解決頁面刷新 404 問題
}
location ^~ /api { # ^~/api/表示匹配前綴為api的請求
# 跨域問題
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
proxy_pass http://www.test.com:5000/api/; # 注:proxy_pass的結尾有/接谨, -> 效果:會在請求時將/api/*后面的路徑直接拼接到后面
}
代理https請求&轉發(fā)
因為微信公眾號的回調只能調用https摆碉,有些時候可能會用到。
這里需要自己了解一下https脓豪,簡而言之需要證書巷帝,針對某一個url,這里展示一個代理示例
server {
listen 443 ssl;
server_name xxx.somliy.top; # https的url
# 申請https證書會給這兩個文件
ssl on;
ssl_certificate /etc/nginx/test/xxx.somliy.top_bundle.crt;
ssl_certificate_key /etc/nginx/test/xxx.somliy.top.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
location / {
proxy_http_version 1.1; #代理使用的http協(xié)議
proxy_set_header Host $host; #header添加請求host信息
proxy_set_header X-Real-IP $remote_addr; # header增加請求來源IP信息
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 增加代理記錄
proxy_pass http://x.x.x.x:8080; # 代理到的真正地址
}
}
FAQ
前端部署后扫夜,刷新404問題
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html; # 解決頁面刷新 404 問題
}
代理配置中楞泼,location的url尾部的/
需不需要
簡單測試了一下驰徊,若是僅僅前端代理,是沒有影響的堕阔,無論是root還是alias
那區(qū)別是什么呢棍厂,當代理的內容為地址時,http://test.com/dir
- http://test.com/dir 代表文件dir
- http://test.com/dir/ 代表目錄dir
url尾部的/
表示目錄超陆,沒有/
表示文件牺弹,是否需要加,根據(jù)情況選擇
代理到后端時时呀,不認識代理標記
使用^~
當做前綴张漂,正則匹配然后代理
location ^~ /api/ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
proxy_pass http://localhost:82/;
}