【发布时间】:2019-03-07 11:14:06
【问题描述】:
我有一个 Nginx 服务器配置,它已经将 http 请求重定向到 https,但我正在尝试找出修改此代码或完全更改它以将 http 请求重定向到 https 的正确方法和在 url 不包含时添加尾部正斜杠 /。这些重定向还应确保尽管重定向仍然存在查询参数。我已经看到我的类似问题的大多数答案都引用了^/(.*)/$ 作为添加正斜杠的代码,但我不确定如何使用它以及是否应该使用rewrite 而不是if。谁能帮我解惑?
nginx.config:
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
server {
listen 8080;
if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }
location / {
proxy_pass http://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
gzip on;
gzip_comp_level 4;
gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}
目标:
http://www.example.com/page-one -> http://example.com/page-one/
http://example.com/page-two -> https://www.example.com/page-two/
https://www.example.com/page-three?page=2 -> https://www.example.com/page-three/?page=2
【问题讨论】:
-
rewrite指令会将查询字符串附加到重写的 URL,除非添加了尾随?。 -
知道了。很高兴知道。你知道正确的
rewrite方法来实现我上面的要求吗?
标签: nginx redirect url-rewriting