简体   繁体   English

将所有http重定向到nginx中的https,除了一个文件

[英]Redirect all http to https in nginx, except one file

I am currently running my site on http, and want to move it over to https such that nginx handles the redirection automagically. 我目前在http上运行我的网站,并希望将其移至https,以便nginx自动处理重定向。 This is fairly trivial to do, I guess. 我猜这很简单。

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. 但是,有一个文件(由于多种原因)是从其他站点进行热链接的,其中一些是通过http进行的,有些是通过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. 我想确保该文件在http和https上均可用,以确保浏览器不会抱怨“混合内容”对话框。 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

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规则应显示:“如果请求已通过https进行,则一切正常,只需反向代理即可。否则,将所有请求从http重定向到https,除非请求了此文件,在这种情况下,请不要请勿执行此类HTTP-> https重定向。”

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.) (很抱歉,但是我对nginx的配置还不够熟练。)

This is what I did, which works: 这是我所做的,并且有效:

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;
    }
}

This one handles only http requests and serves the said file - otherwise redirects to https. 这个仅处理http请求并提供所述文件-否则重定向到https。 Define your ssl server block, which will serve all https requests. 定义您的ssl服务器块,它将服务于所有https请求。

server {
   listen         443;
   server_name    example.com;

   ssl            on;

  # rest of the config
}

This way your script file will be available on http as well as https. 这样,您的脚本文件将在http和https上可用。

Try this: 尝试这个:

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
  }
}
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;

   [....]
}

The above should mostly do the trick if im not wrong 如果我没记错的话,上面的大多数应该可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM