简体   繁体   English

NGINX 重定向 HTTPS 到 HTTPS

[英]NGINX redirect HTTPS to HTTPS

How can I redirect https from one domain to https of another?如何将 https 从一个域重定向到另一个域的 https?

Case description:案例描述:

I own three domains that is directed to the same ip/website in the NS server:我拥有三个域,它们被定向到 NS 服务器中的同一 IP/网站:

  1. example1.com示例1.com
  2. example2.com示例2.com
  3. example3.com例子3.com

I paid only for one SSL licence that initially was issued for the first domain, so when I bought the second domain, I redirected all traffic to https://example1.com我只支付了最初为第一个域颁发的 SSL 许可证,因此当我购买第二个域时,我将所有流量重定向到https://example1.Z4D236D9A2D102C5FE6AD1C50DA4BEC50

Lately, I wanted to redirect all calls to a new domain: https://example3.com .最近,我想将所有调用重定向到一个新域: https://example3.com I reissued the old SSL licence for the new domain and installed it successfully.我为新域重新颁发了旧的 SSL 许可证并成功安装。 The last part is redirecting all traffic to the new domain.最后一部分是将所有流量重定向到新域。

Now, all traffic from http is redirect well, but http s ( https://example1.com and https://example2.com ) is not directed at all, and results in working "Not secure" page. Now, all traffic from http is redirect well, but http s ( https://example1.com and https://example2.com ) is not directed at all, and results in working "Not secure" page.

When I try to listen to 443 SSL and redirect if from 1&2 name servers to the third one and run service nginx restart , I get:当我尝试收听443 SSL并从 1&2 名称服务器重定向到第三个并运行service nginx restart时,我得到:

Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

When I run systemctl status nginx.service , I get:当我运行systemctl status nginx.service时,我得到:

nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2020-05-24 17:53:24 UTC; 4min 5s ago
  Process: 14068 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 13952 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 14073 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
 Main PID: 13957 (code=exited, status=0/SUCCESS)

My guess I'm doing something wrong and redirecting 443 SSL twice is not allowed.我猜我做错了什么并且不允许重定向443 SSL两次。

Code代码

This is my conf that is working without redirecting https example1.com and example2.com:这是我的 conf,无需重定向 https example1.com 和 example2.com 即可工作:

On /etc/nginx/nginx.conf I have include /etc/nginx/sites-enabled/*;/etc/nginx/nginx.conf我已经include /etc/nginx/sites-enabled/*; . .

On /etc/nginx/sites-enabled/example3.com.conf I wrote this:/etc/nginx/sites-enabled/example3.com.conf我写了这个:

server {
    listen 80;
    server_name example3.com wwww.example3.com example1.com www.example1.com example2.com www.example2.com;
    rewrite ^/(.*) https://example3.com/$1 permanent;
}


server {
    listen 443 ssl;
    server_name example3.com;
    ssl_certificate /etc/ssl/certs/ssl-bundle.crt;
    ssl_certificate_key /etc/ssl/websitessl/example3.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
}

This is maybe duplication of this: HTTPS to HTTPS redirect Nginx But I failed to make it work.这可能是重复的: HTTPS 到 HTTPS 重定向 Nginx但我未能使其工作。

Thank's to @RichardSmith, I learned that in order to redirect HTTPS to HTTPS without "Page not secure" warning, you should put the ssl_ statements out of the server blocks so all the domains will be included.感谢@RichardSmith,我了解到为了在没有“页面不安全”警告的情况下将 HTTPS 重定向到 HTTPS,您应该将 ssl_ 语句从服务器块中取出,以便包含所有域。

ssl_certificate /etc/ssl/certs/ssl-bundle.crt;
ssl_certificate_key /etc/ssl/websitessl/example3.com.key;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

server {
        listen  80 default_server;
        listen  443 ssl default_server;
        return  301 https://example3.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example3.com;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
}

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

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