我刚刚配置了 nginx 将 http 重定向到 https,就像如何在 nginx 中强制或重定向到 SSL?通过使用:
server {
listen 80;
server_name rsm.website www.rsm.website;
return 301 https://www.rsm.website$request_uri;
}
重定向似乎成功了一半,因为我确实被重定向到了 https,但似乎丢失了路径$request_uri
。因此,如果用户访问rsm.website/foo/faa
它,总是被重定向到https://rsm.website
我怎样才能使 $request_uri 变量发挥作用?
编辑:
重定向似乎有效www.rsm.website/foo
。只有当没有www.
之前的内容时才会失败rsm.website
这是另一个服务器块。(我只有两个)
server {
listen 443 ssl;
server_name www.rsm.website;
ssl_certificate /etc/nginx/ssl/certificate.wwww.rsm.website.crt;
ssl_certificate_key /etc/nginx/ssl/www.rsm.website.deprotected.key;
client_max_body_size 4G;
access_log /web/logs/nginx-access.log;
error_log /web/logs/nginx-error.log;
location /static/ {
alias /web/static/;
}
location /media/ {
alias /web/media/;
}
location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;
# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://rsmweb_app_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /web/static/;
}
}
答案1
虽然您的主机www.rsm.website
在 DNS 中解析为您的服务器的 IP 地址,但您的主机rsm.website
却没有。它完全由另一台服务器应答。
似乎正在回答一些由 Namecheap 托管的服务器,它返回到 www. 的重定向,但删除了路径和查询字符串。它似乎无法保留它们。
您可以通过修复 DNS 记录来解决此问题,使其成为指向您服务器的 A 记录,而不是指向 Namecheap 的损坏重定向器的记录。