首页 > 解决方案 > Nginx 重定向问题

问题描述

我在 ubunto 18.4 上安装了 nginx,并在 /var/www/html 路径中创建了带有“你好世界”消息的 html 页面。

我创建自签名 SSL 证书,使用本指南链接

我想将所有流量从 HTTP 重定向到 HTTPS。

这是 /etc/nginx/sites-available/default 文件配置:

server {
            listen 443 ssl;
            listen [::]:443 ssl;
            include snippets/self-signed.conf;
            include snippets/ssl-params.conf;
            
            root /var/www/html;
    
            # Add index.php to the list if you are using PHP
            index index.html index.htm index.nginx-debian.html;
    
            server_name _;
    
            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
}
    
             
    
    server {
        listen 80;
        listen [::]:80;
        server_name _;
        return 301 https://35.180.25.62$request_uri; 
}

问题:

在浏览器中,当我输入https://35.180.25.62 - 我得到“万圣节”页面。(好的)

但是,当我输入http://35.180.25.62 - 我没有得到“神圣世界”页面。(坏的)

我希望当我输入http://35.180.25.62时- nginx 将使用 ssl 并向我显示“万圣节”页面。

我也注意到——

1.当我在浏览器中输入http://35.180.25.62时,我在浏览器开发者工具(f12)中检查,重定向带我到5.180.25.62 insted 35.180.25.62!!! 为什么? 看图片

2.只有当我输入http://35.180.25.62/index.html - 重定向很好,我得到了“万圣节”页面。为什么当我输入https://35.180.25.62我不必写 'index.html' ,但在 http 我有?

标签: ubuntunginxredirectssl-certificatenginx-config

解决方案


推荐阅读