http - 将非 www 重定向到 https 时出错

标签 http ssl nginx https

我想重定向到https://example.com 此重定向工作正常:

http://www.example.com -> https://example.com
https://www.example.com -> https://example.com

但这不起作用:

http://example.com -> https://example.com

这是我的 nginx 完整配置:

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

server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        root /var/www/laravel/public;
        index index.php index.html index.htm;

        server_name example.com;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                allow ::1;
                deny all;
        }

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

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

server {
        listen 443 ssl;

        server_name example.com;
        add_header Strict-Transport-Security "max-age=31536000";

        ssl_certificate /home/user/example.com.chained.crt;
        ssl_certificate_key /home/user/example.com.key;
}

我看到很多重定向的示例,但服务器出现错误 500 并且重定向过多。

最佳答案

将 example.com 添加到 server_name

希望对您有所帮助。

关于http - 将非 www 重定向到 https 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40478432/

相关文章:

用于上传文件的 Java HTTP 客户端 (multipart/form-data)

python - 在本地 MediaWiki 中使用 mwclient 时出现 httpError

php - 如何在某些页面上强制使用 HTTPS? (PHP)

python - Flask 应用程序回溯未显示在服务器日志中

node.js - 在实时 Webbynode 服务器上仅看到 "Welcome to socket.io."

apache - 将 documentroot 重定向到 conf apache 或 .htaccess 中的另一个文件夹

java - 在 Android 中访问 Earthquake API。未知主机异常

.net - 从私有(private)订阅源在 Docker 上恢复 Nuget

ssl - 'Client Hello' 请求中的 FreeSWITCH TLS 错误

ruby-on-rails - Nginx 配置 : one Server name and two Applications