NGINX 上的两个带 SSL 的网站。序列和重定向

NGINX 上的两个带 SSL 的网站。序列和重定向

我有两个在 NGINX 上使用 SSL 的网站。

什么时候

curl -I https://www.1111.com answer: https://1111.com

但当

curl -I https://www.2222.com answer: https://1111.com
curl -I https://2222.com answer https://2222.com

在 nginx.conf 中我尝试改变 /import *.conf 的序列,当我在 1111.com 之前设置 2222.com > 答案发生了相反的变化。

1111.conf

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

server {
    listen 443 ssl http2;
    server_name 1111.com;
    root /usr/share/nginx/1111.com;
    index index.php;
...

2222.conf类似

我如何才能重定向此网站上的所有 https:// www.* 和 http:// www.*,而不重定向其他网站?

答案1

是啊!正确的配置:

server {
  listen   80;
  server_name site.ru www.site.ru;
  rewrite  ^(.*) https://$server_name$1 permanent;
}
server {
  listen   443   ssl http2;
  include ssl/ssl_site.ru;
  server_name  www.site.ru;
  rewrite ^(.*) https://site.ru$1 permanent;
}
server {
  listen   443   ssl http2;
  server_name site.ru;
  include ssl/ssl_site.ru;

... other config string ...

答案2

这是有点棘手,但你当然可以做到。

map $http_host $new {
  'www.abc.com' '1';
  'www.xyz.com' '2';
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    if ($http_x_forwarded_proto != 'https') {
          rewrite ^(.*) https://$host$1 redirect;
    }
}

server {
  if ($new != '1') {
        server_name www.xyz.com;

        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;

        ssl_certificate  /etc/ssl/certs/s2.pem;
        ssl_certificate_key /etc/ssl/certs/s2.key;

        root /var/www/html/site2;

        index index.html index.htm index.nginx-debian.html;

        location / {

    }
  }

  if ($new != '2') {
        server_name www.abc.com;

        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;

        ssl_certificate  /etc/ssl/certs/s1.pem;
        ssl_certificate_key /etc/ssl/certs/s1.key;

        root /var/www/html/site1;

        index index.html index.htm index.nginx-debian.html;

        location / {

    }
  }
}

这可能是更多信息和 R&C 读取 nginx 映射模块的确切答案

相关内容