我目前在 http 上运行我的站点,并希望将其移至 https,以便 nginx 自动处理重定向。我猜这是相当微不足道的事情。
但是,有一个文件(出于多种原因)是从其他站点热链接的,其中一些站点是通过 http,一些站点是通过 https。我想确保文件在 http 和 https 上都可用,以确保浏览器不会提示“混合内容”对话框。文件的路径看起来像这样:
http(s)://mydomain.com/scripts/[some_sha1_hash]/file.js
所以,nginx 规则应该说:“如果请求已经通过 https,一切都很好,并且只是反向代理它。否则,将所有请求从 http 重定向到 https,除非请求这个文件,在在这种情况下不要做任何这样的 http->https 重定向。”
任何人都可以告诉我在哪里可以了解这样的配置,或者帮助我了解配置本身吗?提前致谢。 (抱歉,我对nginx的配置还不够熟练。)
最佳答案
这就是我所做的,它有效:
server {
listen 80;
server_name example.com;
charset utf-8;
access_log /var/www/path/logs/nginx_access.log;
error_log /var/www/path/logs/nginx_error.log;
location /path/to/script.js {
# serve the file here
}
location / {
return 301 https://example.com$request_uri;
}
}
这一个只处理 http 请求并提供所述文件 - 否则重定向到 https。定义您的 ssl 服务器 block ,它将为所有 https 请求提供服务。
server {
listen 443;
server_name example.com;
ssl on;
# rest of the config
}
这样您的脚本文件将在 http 和 https 上可用。
关于http - 在 nginx 中将所有 http 重定向到 https,除了一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426299/