nginx rewrite配置问题
高洛峰
高洛峰 2016-10-22 15:20:37
0
1
818

最近用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收到的请求路径为/呢?


高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

répondre à tous(1)
三叔

不知道我理解的对不对。

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 用了 .+ 导致的吧。

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!