Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx: Preserve Post

Tags:

nginx

My setup looks as following:

example.com/de is now example.de, example.com/en is now example.com. I'm using nginx latest version.

I need to preserve the post that is sent via iOS app to http://www.example.com/de/api/uploadPicture, which is now http://www.example.com/api/uploadPicture.

I found out that I need to use location instead of rewrite rule.

This is what I have in the .com block:

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

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

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

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

This is the mentioned part:

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/$1 redirect;
}

Thanks for your appreciated help.

EDIT: I've adjusted with the given solution. This works, but not in combination with the rewrite rules:

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/$1 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/$1 redirect;
    }    


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


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

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

    rewrite ^/de(.*)$ http://www.example.de$1 permanent;   
    rewrite ^/en(.*)$ http://www.example.com$1 permanent;
like image 297
sullivan Avatar asked Oct 21 '25 13:10

sullivan


1 Answers

Using 307 redirects for POST requests will allow you to keep the POST data. In your case something like this should work fine:

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/$1 redirect;
}
like image 90
Ivan Tsirulev Avatar answered Oct 23 '25 02:10

Ivan Tsirulev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!