配置代理
代理配置很簡單,只需要在location內部增加你需要的代理的網址以及端口行了。
proxy_pass http://ip:端口;
這里以localhost默認監聽80端口為例,我們使用nginx代理到blog.test:8080這里。
配置如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://blog.test:8080;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
配置好之后,我們打開網站就是這樣的情況了

多個代理配置
多個nginx代理,需要配置多個server,復制上面server,粘貼在下面。不要在一個server改動。
如下,我們想讓當前系統的所有的8088端口轉發到192.168.2.1:8088端口。
所有的8099端口轉發到172.122.1.58:3306端口。
可以這寫:
- 所有8088端口轉發到192.168.2.1:8088端口,添加如下代理
# 監聽所有的端口為8088 server { listen *:8088; server_name blog.test; location / { proxy_pass http://192.168.2.1:8088; root html; index index.html index.htm; } } - 8099端口轉發到172.122.1.58:3306端口,添加如下代理
# 監聽所有的端口為8099 server { listen *:8099; server_name blog.test; location / { proxy_pass http://172.122.1.58:3306; root html; index index.html index.htm; } }
