Rak*_*Pai 10 ssl https redirect http nginx
我目前正在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;
}
}
Run Code Online (Sandbox Code Playgroud)
这个只处理http请求并提供所述文件 - 否则重定向到https.定义您的ssl服务器块,它将为所有https请求提供服务.
server {
listen 443;
server_name example.com;
ssl on;
# rest of the config
}
Run Code Online (Sandbox Code Playgroud)
这样,您的脚本文件将在http和https上可用.
小智 5
尝试这个:
server {
listen 80; ssl off;
listen 443 ssl;
server_name example.com;
# <ssl settings>
# ... other settings
location = /scripts/[some_sha1_hash]/file.js {
# Empty block catches the match but does nothing with it
}
location / {
if ($scheme = "http") {
rewrite ^ https://$http_host$request_uri? permanent;
}
# ... other settings
}
}
Run Code Online (Sandbox Code Playgroud)
小智 -2
server {
listen 80;
server_name my.domain.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443;
server_name my.domain.com;
ssl on;
[....]
}
Run Code Online (Sandbox Code Playgroud)
如果我没有错的话,上面的内容应该可以解决大部分问题