我的设置如下:
example.com/de现在是example.de,example.com/en现在是example.com。我使用的是nginx最新版本。
我需要保留通过iOS应用发送到http://www.example.com/de/api/uploadPicture的帖子,现在是http://www.example.com/api/uploadPicture。
我发现我需要使用位置而不是重写规则。
这就是我在.com块中所拥有的:
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;
}
这是上面提到的部分:
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;
}
感谢您的帮助。
编辑:我已根据给定的解决方案进行了调整。这有效,但不能与重写规则结合使用: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;
答案 0 :(得分:7)
对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/$1 redirect;
}