Nginx:保留 Post

Nginx: Preserve Post

我的设置如下所示:

example.com/de 现在是 example.de,example.com/en 现在是 example.com。我正在使用 nginx 最新版本。

我需要保留通过 iOS 应用程序发送到 http://www.example.com/de/api/uploadPicture, which is now http://www.example.com/api/uploadPicture 的 post。

我发现我需要使用位置而不是重写规则。

这是我在 .com 块中的内容:

location /de/shop {
    rewrite ^/de/shop/(.*) http://www.example.de/ redirect;
}    

location /de {
    rewrite ^/de/(.*) http://www.example.de/ redirect;
}    

location /en/shop {
    rewrite ^/en/shop/(.*) http://www.example.com/ redirect;
}    

location /en {
    rewrite ^/en/(.*) http://www.example.com/ redirect;
}

这是提到的部分:

location /de/api/uploadPicture {

    # Not sure how to use this one
    #proxy_pass http://myproject$uri$is_args$args;
    #proxy_redirect  http://localhost:8080//;    

    # This works, but looses the post data
    rewrite ^/de/api/(.*) http://www.example.com/api/ redirect;
}

感谢您的帮助。

编辑:我已经使用给定的解决方案进行了调整。这有效,但不能与重写规则结合使用:

server {
    client_max_body_size 20M;
    listen 80;
    listen 443 ssl;    

    ssl_certificate /home/ib/ssl/ib.pem;
    ssl_certificate_key /home/ib/ssl/ib.key;    

    server_name www.example.com;
    location / {
        proxy_pass http://myproject;
        error_page 500 502 503 504 /error.html;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-REAL-SCHEME $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }    

    location /error.html {
            root /home/ib/error;
    }    

    location ~ ^/de/api/(?<method>.*)$ {
            if ($request_method = POST) {
                return 307 http://www.example.de/api/$method$is_args$args;
            }    

            # non-POST requests
            rewrite ^/de/api/(.*) http://www.example.com/api/ redirect;
    }    

    location ~ ^/en/api/(?<method>.*)$ {
            if ($request_method = POST) {
                return 307 http://www.example.com/api/$method$is_args$args;
            }    

            # non-POST requests
        rewrite ^/de/api/(.*) http://www.example.com/api/ redirect;
    }    


    rewrite ^/de/shop/(\d+)/(.*) http://www.example.de// permanent;
    rewrite ^/en/shop/(\d+)/(.*) http://www.example.com// permanent;    


    rewrite ^/de/(.*)-(\d+) http://www.example.de/-.html permanent;
    rewrite ^/en/(.*)-(\d+) http://www.example.com/-.html permanent;    

    rewrite ^/de/shop/(.*)-(\d+) http://www.example.de/-.html permanent;
    rewrite ^/en/shop/(.*)-(\d+) http://www.example.com/-.html permanent;    

    rewrite ^/de(.*)$ http://www.example.de permanent;   
    rewrite ^/en(.*)$ http://www.example.com permanent;

对 POST 请求使用 307 重定向将允许您保留 POST 数据。在您的情况下,这样的事情应该可以正常工作:

location ~ ^/de/api/(?<method>.*)$ {
    if ($request_method = POST) {
        return 307 http://www.example.com/api/$method$is_args$args;
    }

    # You can keep this for non-POST requests
    rewrite ^/de/api/(.*) http://www.example.com/api/ redirect;
}