将所有http重定向到nginx中的https,但一个文件除外 [英] Redirect all http to https in nginx, except one file

查看:147
本文介绍了将所有http重定向到nginx中的https,但一个文件除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在http上运行我的网站,并希望将其移至https,以便nginx自动处理重定向。我猜这很简单。

I am currently running my site on http, and want to move it over to https such that nginx handles the redirection automagically. This is fairly trivial to do, I guess.

然而,有一个文件(由于多种原因)与其他网站热链接,其中一些已经结束http和一些通过https。我想确保该文件可通过http和https使用,以确保浏览器不会抱怨混合内容对话框。该文件的路径如下所示:

However, there is one file that (for several reasons) is hot-linked from other sites, some of which are over http and some over https. I want to ensure that the file is available over both http and https, so as to ensure that browsers don't complain with the "mixed content" dialog. The path of the file looks something like this:

http(s):// mydomain.com/scripts/ [some_sha1_hash] /file.js

http(s)://mydomain.com/scripts/[some_sha1_hash]/file.js

因此,nginx规则应该说:如果请求已经通过https,一切都很好,只需反向代理它。否则,将所有请求从http重定向到https,除非请求这个文件,在这种情况下不要做任何这样的http-> https重定向。

So, the nginx rule should say: "If the request is already over https, everything is sweet, and just reverse-proxy it. Otherwise, redirect all requests from http to https, except if this one file is requested, in which case don't do any such http->https redirect."

任何人都可以告诉我在哪里可以了解这样的配置,还是帮我配置本身?提前致谢。 (对不起,但我在nginx配置上还不够熟练。)

Can anyone either tell me where to look to learn about such a config, or help me with the config itself? Thanks in advance. (I'm sorry, but I'm not skilled enough yet at nginx configuration.)

推荐答案

这就是我做的,有效:

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服务器块,它将为所有https请求提供服务。

This one handles only http requests and serves the said file - otherwise redirects to https. Define your ssl server block, which will serve all https requests.

server {
   listen         443;
   server_name    example.com;

   ssl            on;

  # rest of the config
}

这样你的脚本文件将在http和https上提供。

This way your script file will be available on http as well as https.

这篇关于将所有http重定向到nginx中的https,但一个文件除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆