Nginx HTTPS SSL重定向在Ubuntu 18.04中不起作用

时间:2019-06-13 17:38:08

标签: nginx ubuntu-18.04 nginx-config

我已经为使用SSL配置Nginx服务器而感到疲倦,但是该站点未打开,但是使用https://它已正常打开。

这是我的Nginx配置:

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    client_max_body_size 20M;

    root /var/www/mysite.in/site;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

    server_name _;

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

    # pass PHP scripts to FastCGI server
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    }

    listen 443;
    listen [::]:443;

    server_name www.mysite.com;

    #ssl on;
    ssl_certificate /etc/nginx/ssl/mysite.com.chained.crt;
    ssl_certificate_key /etc/nginx/ssl/main_private.key;
}

SSL是在GoDaddy中生成的,我已经找到了很多解决方案,但是到目前为止,它们都没有用。

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

您必须在配置中创建两个服务器(http和https),并创建从http到https的重定向:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name www.mysite.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443;
    listen [::]:443;

    server_name www.mysite.com;   

    client_max_body_size 20M;

    root /var/www/mysite.in/site;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

    server_name _;

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

    # pass PHP scripts to FastCGI server
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    }

    ssl on;
    ssl_certificate /etc/nginx/ssl/mysite.com.chained.crt;
    ssl_certificate_key /etc/nginx/ssl/main_private.key;
}