Heim  >  Fragen und Antworten  >  Hauptteil

nginx rewrite配置问题

最近用flask写了一个接口,在配置nginx转发的时候遇到了点小问题,nginx配置如下:

server {
    listen 80;
    server_name xxx.xxx.xxx;

    location /upload_api {
        rewrite ^/upload_api/(.+)$ /$1 break;
        proxy_pass http://127.0.0.1:8082;
    }
    
}

flask收到的请求路径确实

127.0.0.1 - - [21/Oct/2016 16:31:36] "GET /upload_api HTTP/1.0" 404 -

我怎么能让用户请求/upload_api的时候flask收到的请求路径为/呢?


高洛峰高洛峰2918 Tage vor819

Antworte allen(1)Ich werde antworten

  • 三叔

    三叔2016-10-22 15:21:23

    不知道我理解的对不对。

    server {
        listen 80;
        server_name xxx.xxx.xxx;
    
        location /upload_api {
            rewrite ^/upload_api/(.+)$ /$1 break;
            proxy_pass http://127.0.0.1:8082;
        }
        
    }

    这是你的 nginx 配置,这句话 rewrite ^/upload_api/(.+)$ /$1 break 的意思是当你访问xxx.xxx.xxx/upload_api/pwd 的时候,会重定向到 xxx.xxx.xxx/pwd

    你为什么用 .+ 而不是 .*,在正则中 + 代表一次或多次,* 代表 0 到多次,我觉得用 * 比较合适吧

    既然用了 break,后面的内容就可以删了

    127.0.0.1 - - [21/Oct/2016 16:31:36] "GET /upload_api HTTP/1.0" 404 -

    不懂 flask,但是你这条记录好像是访问 /upload_api 出现了 404,应该是你 rewrite 用了 .+ 导致的吧。

    Antwort
    0
  • StornierenAntwort