nginx各種代理配置大全
以下展示不同情況下的配置:location路徑、root路徑、alias路徑、proxy_pass代理路徑。
通過(guò)這幾個(gè)配置路徑地址對(duì)比,建議location后面都帶上斜杠。
1. 基礎(chǔ)配置說(shuō)明
# 進(jìn)程數(shù)量
worker_processes 1;
events {
# 最大連接數(shù)量
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;2. 演示如何強(qiáng)制http跳轉(zhuǎn)https
server {
listen 80;
server_name test.com;
# http強(qiáng)制跳轉(zhuǎn)到https
rewrite ^(.*)$ https://$server_name$1 permanent;
}3. 演示如何配置微信支付的校驗(yàn)文件
server {
listen 80;
server_name localhost;
# 默認(rèn)根路徑
location / {
root index.html;
}
# 微信支付校驗(yàn)文件,可以直接配置訪問(wèn)名稱(chēng)
location ^~/MP_verify_2g3uEjrB5B2LIbNl.txt {
alias /home/MP_verify_2g3uEjrB5B2LIbNl.txt;
}
# 微信支付校驗(yàn)文件,也可以通過(guò)正則配置
location ~^/MP_verify_[a-zA-Z0-9]*\.(txt)$ {
root /home/;
rewrite ^/home/(.txt)$ /home/$1 last;
}
}4. 演示root和alias兩種配置靜態(tài)資源的區(qū)別
server {
listen 80;
server_name localhost;
# 用root方式,location中的路徑會(huì)拼加到root的地址后面
# 請(qǐng)求路徑為:http://localhost:8080/files/index.jpg 實(shí)際訪問(wèn)為:/home/files/index.jpg
location ~^/files/ {
root /home/;
index index.html index.htm;
}
# 用alias方式,location中的路徑不會(huì)拼加到alias的地址后面
# 這請(qǐng)求路徑為:http://localhost:8080/files/index.jpg 實(shí)際訪問(wèn)為:/home/index.jpg
location ~^/files/ {
alias /home/;
index index.html index.htm;
}
}5.演示請(qǐng)求后臺(tái)接口代理配置
server {
listen 8080;
server_name localhost;
#################### 第一種場(chǎng)景(代理地址不加斜杠) ####################
# 請(qǐng)求路徑為:http://127.0.0.1:8080/api/getUser 實(shí)際代理為:http://127.0.0.1:8000/api/getUser
location ^~/api/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $http_host; #后臺(tái)可以獲取到完整的ip+端口號(hào)
proxy_set_header X-Real-IP $remote_addr; #后臺(tái)可以獲取到用戶(hù)訪問(wèn)的真實(shí)ip地址
}
# 請(qǐng)求路徑為:http://127.0.0.1:8080/api/getUser 實(shí)際指向?yàn)椋篽ttp://127.0.0.1:8000/api/getUser
location ^~/api {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $http_host; #后臺(tái)可以獲取到完整的ip+端口號(hào)
proxy_set_header X-Real-IP $remote_addr; #后臺(tái)可以獲取到用戶(hù)訪問(wèn)的真實(shí)ip地址
}
#################### 第二種場(chǎng)景(代理地址+斜杠) ####################
# 請(qǐng)求路徑為:http://127.0.0.1:8080/api/getUser 實(shí)際代理為:http://127.0.0.1:8000/getUser
location ^~/api/ {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $http_host; #后臺(tái)可以獲取到完整的ip+端口號(hào)
proxy_set_header X-Real-IP $remote_addr; #后臺(tái)可以獲取到用戶(hù)訪問(wèn)的真實(shí)ip地址
}
# 請(qǐng)求路徑為:http://127.0.0.1:8080/api/getUser 實(shí)際代理為:http://127.0.0.1:8000//getUser
location ^~/api {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $http_host; #后臺(tái)可以獲取到完整的ip+端口號(hào)
proxy_set_header X-Real-IP $remote_addr; #后臺(tái)可以獲取到用戶(hù)訪問(wèn)的真實(shí)ip地址
}
#################### 第三種場(chǎng)景(代理地址+后綴) ####################
# 請(qǐng)求路徑為:http://127.0.0.1:8080/api/getUser 實(shí)際代理為:http://127.0.0.1:8000/user/getUser
location ^~/api {
proxy_pass http://127.0.0.1:8000/user;
proxy_set_header Host $http_host; #后臺(tái)可以獲取到完整的ip+端口號(hào)
proxy_set_header X-Real-IP $remote_addr; #后臺(tái)可以獲取到用戶(hù)訪問(wèn)的真實(shí)ip地址
}
# 請(qǐng)求路徑為:http://127.0.0.1:8080/api/getUser 實(shí)際代理為:http://127.0.0.1:8000/usergetUser
location ^~/api/ {
proxy_pass http://127.0.0.1:8000/user;
proxy_set_header Host $http_host; #后臺(tái)可以獲取到完整的ip+端口號(hào)
proxy_set_header X-Real-IP $remote_addr; #后臺(tái)可以獲取到用戶(hù)訪問(wèn)的真實(shí)ip地址
}
#################### 第四種場(chǎng)景(代理地址+后綴+斜杠) ####################
# 請(qǐng)求路徑為:http://127.0.0.1:8080/api/getUser 實(shí)際代理為:http://127.0.0.1:8000/user/getUser
location ^~/api/ {
proxy_pass http://127.0.0.1:8000/user/;
proxy_set_header Host $http_host; #后臺(tái)可以獲取到完整的ip+端口號(hào)
proxy_set_header X-Real-IP $remote_addr; #后臺(tái)可以獲取到用戶(hù)訪問(wèn)的真實(shí)ip地址
}
# 請(qǐng)求路徑為:http://127.0.0.1:8080/api/getUser 實(shí)際代理為:http://127.0.0.1:8000/user//getUser
location ^~/api {
proxy_pass http://127.0.0.1:8000/user/;
proxy_set_header Host $http_host; #后臺(tái)可以獲取到完整的ip+端口號(hào)
proxy_set_header X-Real-IP $remote_addr; #后臺(tái)可以獲取到用戶(hù)訪問(wèn)的真實(shí)ip地址
}
}6.演示前端項(xiàng)目如何部署nginx
server {
listen 8090;
server_name localhost;
# 默認(rèn)訪問(wèn)
# 部署路徑:/home/web/my_demo
# 訪問(wèn)路徑為:http://localhost:8090/
location / {
try_files $uri $uri/ /index.html;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
root /home/web/my_demo/;
index index.html index.htm;
}
# 帶前綴的訪問(wèn)
# 部署路徑:/home/web/my_demo
# 訪問(wèn)路徑為:http://localhost:8090/my_demo/
# 如果location路徑最后沒(méi)有配置斜杠,則瀏覽器輸入訪問(wèn)地址后,路徑最后會(huì)自動(dòng)拼一個(gè)斜杠
location ^~/my_demo/ {
try_files $uri $uri/ /my_demo/index.html;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
root /home/web/;
index index.html index.htm;
}
}
}到此這篇關(guān)于nginx各種代理配置大全的文章就介紹到這了,更多相關(guān)nginx 代理配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx?403?forbidden錯(cuò)誤的原因以及解決方法
yum安裝nginx,安裝一切正常,但是訪問(wèn)時(shí)報(bào)403 forbidden,下面這篇文章主要給大家介紹了關(guān)于Nginx?403?forbidden錯(cuò)誤的原因以及解決方法,需要的朋友可以參考下2022-08-08
Nginx反向代理springboot的jar包過(guò)程解析
這篇文章主要介紹了Nginx反向代理springboot的jar包過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
HAProxy和Nginx搭建負(fù)載均衡器的實(shí)現(xiàn)
負(fù)載均衡器是一個(gè)常用于分布式計(jì)算和網(wǎng)絡(luò)應(yīng)用中的系統(tǒng)組件,主要用于將客戶(hù)端的請(qǐng)求分發(fā)到多個(gè)后端服務(wù)器上,以實(shí)現(xiàn)高可用性、高性能和可擴(kuò)展性,本文主要介紹了HAProxy和Nginx搭建負(fù)載均衡器的實(shí)現(xiàn),感興趣的可以了解一下,感興趣的可以了解一下2023-11-11
Nginx內(nèi)網(wǎng)單機(jī)反向代理的實(shí)現(xiàn)
本文主要介紹了Nginx內(nèi)網(wǎng)單機(jī)反向代理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
解決systemctl reload nginx重啟Nginx服務(wù)報(bào)錯(cuò):Job for&n
文章描述了通過(guò)`systemctl status nginx.service`發(fā)現(xiàn)Nginx服務(wù)未啟動(dòng),啟動(dòng)失敗的原因可能是端口號(hào)被占用,使用`netstat -ntlp | grep 80`命令找到了占用80端口的進(jìn)程(PID為7008),通過(guò)`kill 7008`停止了該進(jìn)程,然后重新啟動(dòng)Nginx2025-01-01
利用nginx+lua+redis實(shí)現(xiàn)反向代理方法教程
這篇文章主要給大家介紹了利用nginx+lua+redis實(shí)現(xiàn)反向代理方法教程,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05

