Home  >  Q&A  >  body text

linux - Problems with redirection of http and https first-level domain names and second-level domain names in Nginx

~~I have a question for you seniors:
Today I enabled SSL support for the site. I want to do this:
No matter whether you visit https://domain.com or http://domain.com or http://www.domain.com, 301 automatically jumps to https://www.domain.com, that is to say, https://www.domain.com is the only access Url
. How should I write it? Configuration? This is how I do it:

But access will go wrong. Shows connection failure.
Remove

server {
    listen 443;
    server_name domain.com;
    return 301...
}

This section can be accessed normally, but accessing https://domain.com will not jump to https://www.domain.com

Is there any solution?
~~
Resolved: https://www.iots.vip/post/ngi...

PHPzPHPz2713 days ago968

reply all(3)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:21:44

    The connection failed. What is the http status code? I feel like there is nothing wrong with this configuration.

    server {
            listen 443 ssl;
            server_name www.test.com;
            root html;
            index index.html index.htm;
            ssl_verify_client off;
            ssl_certificate /etc/nginx/test.com.crt;
            ssl_certificate_key /etc/nginx/test.com.pem;
            ssl_client_certificate /etc/nginx/ca.com.crt;
            location / {
                    proxy_set_header X-http-type https;
                    proxy_set_header X-request-ip $remote_addr;
                    proxy_pass http://127.0.0.1:9004/;
            }
            ssl_session_timeout 5m;
            ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
            ssl_prefer_server_ciphers on;
        }
    server {
            listen 443;
            server_name test.com;
            return 301 https://www.test.com$request_uri;
    
        }
    server {
            listen 80;
            server_name test.com www.test.com;
            return 301 https://www.test.com$request_uri;
    
        }
        

    It’s no problem for me, this way, I can jump.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:21:44

    The connection has been reset. There is no status code.

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:21:44

    Regarding the problem of domain.com jumping to www.domain.com, you can set it directly during domain name resolution

    www A record 10.10.10.10
    @ cname www.baidu.com. (remember there is a "dot" here)

    reply
    0
  • Cancelreply