Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redirect Nginx将https重定向到非www不工作_Redirect_Nginx - Fatal编程技术网

Redirect Nginx将https重定向到非www不工作

Redirect Nginx将https重定向到非www不工作,redirect,nginx,Redirect,Nginx,我已经查看了堆栈溢出和其他站点,但似乎无法使此重定向正常工作。一切正常,除了: 以下工作: http://example.com -> https://example.com http://www.example.com -> https://example.com 这不会重定向到非www(它将www保持在原位): www和非www https版本似乎都能按原样解析。有什么想法吗 以下是当前配置: # Redirect WWW to NON-WWW server { # S

我已经查看了堆栈溢出和其他站点,但似乎无法使此重定向正常工作。一切正常,除了:

以下工作:

http://example.com -> https://example.com
http://www.example.com -> https://example.com
这不会重定向到非www(它将www保持在原位):

www和非www https版本似乎都能按原样解析。有什么想法吗

以下是当前配置:

# Redirect WWW to NON-WWW
server {
    # Server host
    server_name www.example.com;

    # Server ports
    listen 80;
    listen 443;
    listen [::]:80;
    listen [::]:443;

    # SSL
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # Redirect
    return 301 https://example.com$request_uri;
}

# Redirect HTTP to HTTPS
server {
    # Server host
    server_name example.com;

    # Server port
    listen 80;
    listen [::]:80;

    # Redirect
    return 301 https://example.com$request_uri;
}

# Main Block
server {
    # Server host
    server_name example.com;

    # Server port
    listen 443 ssl;
    listen [::]:443;

    # SSL
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    root /var/www/example.com;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
     }

    location ~ /.well-known {
        allow all;
    }
}

您在第一个服务器块中似乎没有打开
ssl
。@RichardSmith当您说打开是什么意思?在第一个服务器块中,我正在侦听端口443,并且有ssl证书-我还需要添加其他证书吗?等等,为什么要重定向
https://www.example.com -> https://www.example.com
?你是说
https://www.example.com -> https://example.com
?此外,我相信理查德·史密斯的意思是。@grochmal正确!我刚刚在我的帖子里修正了@grochmal I在上添加了ssl;修复了最后一个块中ssl密码的语法错误,一切正常!谢谢你们两位!您在第一个服务器块中似乎没有打开
ssl
。@RichardSmith当您说打开是什么意思?在第一个服务器块中,我正在侦听端口443,并且有ssl证书-我还需要添加其他证书吗?等等,为什么要重定向
https://www.example.com -> https://www.example.com
?你是说
https://www.example.com -> https://example.com
?此外,我相信理查德·史密斯的意思是。@grochmal正确!我刚刚在我的帖子里修正了@grochmal I在上添加了ssl;修复了最后一个块中ssl密码的语法错误,一切正常!谢谢你们两位!
# Redirect WWW to NON-WWW
server {
    # Server host
    server_name www.example.com;

    # Server ports
    listen 80;
    listen 443;
    listen [::]:80;
    listen [::]:443;

    # SSL
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # Redirect
    return 301 https://example.com$request_uri;
}

# Redirect HTTP to HTTPS
server {
    # Server host
    server_name example.com;

    # Server port
    listen 80;
    listen [::]:80;

    # Redirect
    return 301 https://example.com$request_uri;
}

# Main Block
server {
    # Server host
    server_name example.com;

    # Server port
    listen 443 ssl;
    listen [::]:443;

    # SSL
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    root /var/www/example.com;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
     }

    location ~ /.well-known {
        allow all;
    }
}