6.Nginx反向代理

反向代理

xx.xx.xx.xx:80/test 转跳到 linux 127.0.0.1:88服务(其他机器)

# 如果proxy_pass http://127.0.0.1:8081 不加【/】的话,请求地址会变为http://127.0.0.1:8081/api/get/user
location /api/get/user {
  proxy_pass http://127.0.0.1:8081;
}
server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #access_log  "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G"  main;

        location / {
            root   html;
            index  index.html index.htm;
            charset utf-8;
        }
        location /test {
            proxy_pass   http://127.0.0.1:8111/;
        }
}
python代码

另一份port 改为 8111 > 8112

# flask-demo.py / flask-demo1.py
from flask import Flask

app = Flask(__name__)
.route("/")@app
def hello_world():
    return "Hello World!"
@app.route("/test1")
def test1():
    return "8111 test"
@app.route("/test2")
def test2():
    return "8111 test2"
@app.route("/test3")
def test3():
    return "8111 test3"


if __name__ == "__main__":
    app.run(host="127.0.0.1", port=8111)
nginx配置
 server {
        listen       80;
        server_name  47.98.220.24;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.htm;
            charset utf-8;
        }
        location /f1/ {
            proxy_pass   http://127.0.0.1:8111/;
        }
        location /f1/test1/ {
            proxy_pass   http://127.0.0.1:8111/test1;
        }
        location /test2/ {
            proxy_pass   http://127.0.0.1:8111/test2;
        }
        location /f2/ {
            proxy_pass   http://127.0.0.1:8112/;
        }
        location /test3/ {
            proxy_pass   http://127.0.0.1:8112/test3;
        }
}
47.98.220.24:80/ > [html]文件夹下的[index.html或index.htm]
	默认查的是安装路径下的html文件夹[/usr/local/tengine/html]下的[index.html或index.htm]
# flask-demo.py启动的服务
47.98.220.24:80/f1/  >>> 127.0.0.1:8111/
47.98.220.24:80/f1/test1/  >>> 127.0.0.1:8111/test1
47.98.220.24:80/test2/  >>> 127.0.0.1:8111/test2
# flask-demo1.py启动的服务
47.98.220.24:80/f2/  >>> 127.0.0.1:8112/
47.98.220.24:80/test3/  >>> 127.0.0.1:8112/test3
posted @ 2022-06-07 14:33  lxd670  阅读(21)  评论(0编辑  收藏  举报