将httpredirect到https但排除一些

我正在使用nginx,我想知道是否有可能做到这一点。

现在要pipe理我的虚拟主机,我分别在/ etc / nginx / sites-available(在之后激活它们)

mydomain.com subdomain.mydomain.com subdomain1.mydomain.com anotherdomain.com 

所以要将httpredirecthttps,我创build了一个redirect虚拟主机,其中包含:

 server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 301 https://$host$request_uri; } 

mydomain.com和所有其他子域都在https下。

但问题是anotherdomain.com,我不想redirect到https。 在这里,我不知道该怎么做。 我有一个主意 :

仅在包含所有configuration的虚拟主机上创build,然后执行以下操作:

 # Config of anotherdomain.com # Redirect all domain/subdomain after this part to https server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 301 https://$host$request_uri; } # Config of mydomain.com # Config of subdomain.com # Config of subdomain1.com 

这将作为一种重写,就像在CSS?

感谢您的反馈意见。

通常,每个域/子域将有一个转发块。 使用default_server进行redirect并不是我之前看到的,尽pipe在大多数情况下它可能没问题。

因此,不是使用默认服务器,而是按照以下方式定义一个块,无论是针对有问题的域还是针对每个域

 server { listen 80; server_name anotherdomain.com; location / { // whatever } } 

这将覆盖default_server块。 就个人而言,我会为每个网站的域定义转发块,只是因为它看起来更整洁。

 server { listen 80; server_name example.com; return 301 https://example.com$request_uri; } 

default_server是当请求与任何其他虚拟主机不匹配时使用的虚拟主机。

因此,您需要在端口80上定义一个与anotherdomain.com匹配的虚拟主机anotherdomain.com将使用该configuration,而不是您的default_server虚拟主机。