Home  >  Q&A  >  body text

flask - nginx rewrite routing

nginx gunicorn flask configuration
Now I map the /demo route to port 8088, how to rewrite the route

server {
    listen 80;
    server_name my_server;
    
    location /demo {
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://127.0.0.1:8088;
                        
        rewrite ^/demo/(.*)$ / last;
    }
}

But if I write it like this, localhost/demo still doesn’t work, what should I do?

给我你的怀抱给我你的怀抱2713 days ago760

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-16 17:23:44

    Visit my_server/demo and try?

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 17:23:44

    rewrite ^/demo/(.*)$ / last;

    changed to

    rewrite ^/demo/(.*)$ / break;
    Data sources in

    last会对server块重新发起请求,break则使用当前location

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 17:23:44

    If you say you want

    /demo is proxy to 8088, /demo/xx is proxy to 8088/xx

    If

    , you may need to do this:

    server {
        listen 80;
        server_name my_server;
        
        location / {
            proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://127.0.0.1:8088;
                            
            rewrite ^/demo/(.*)$ / break;
        }
    }

    But this can be processed directly in your background program, because the result is that accessing /demo/xx has the same effect as accessing /xx

    reply
    0
  • Cancelreply