nginx反向代理配置 其實很簡單


配置代理

代理配置很簡單,只需要在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;
        }
    }

配置好之后,我們打開網站就是這樣的情況了
blog.test

多個代理配置

多個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;
        }
    }
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM