【问题标题】:Redirect NGINX http to https将 NGINX http 重定向到 https
【发布时间】:2018-01-05 22:54:27
【问题描述】:

我在我的域 memorisemedicine.com 上的 Nginx 服务器上使用 LetsEncrypt。

我在位于 /etc/nginx/sites-available 的“memorise-frontend.conf”文件的末尾添加了一个服务器块,它应该将非 SSL 流量重定向到 SSL (https)。

现在,当我在不使用 https:// 的情况下访问该站点时,我注意到有时我现在被正确重定向了。但值得注意的是,在 Firefox 中,这对我不起作用,我仍然可以访问该域上的 http:// 页面,而我希望这永远不可能。有人知道我的 .conf 文件有什么问题吗?我也尝试编辑 nginx.conf 文件,但似乎它并没有很好地占用服务器块。

server {
charset utf-8;
client_max_body_size 128M;

listen 80; ## listen for ipv4
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6



server_name memorisemedicine.com www.memorisemedicine.com;


root        /srv/memorise/frontend/web;
index       index.php;



# access_log  /path/to/basic/log/access.log;
# error_log   /path/to/basic/log/error.log;

location / {
    # Redirect everything that isn't a real file to index.php
    try_files $uri $uri/ /index.php$is_args$args;
}

# uncomment to avoid processing of calls to non-existing static files by Yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
#    try_files $uri =404;
#}
#error_page 404 /404.html;

# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
    deny all;
}

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    try_files $uri =404;
}

location ~* /\. {
    deny all;
}

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/memorisemedicine.com/fullchain.pem; # man$
 ssl_certificate_key /etc/letsencrypt/live/memorisemedicine.com/privkey.pem; # m$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

 }

 server {
 return 301 https://memorisemedicine.com$request_uri;
 }

【问题讨论】:

  • 您的主服务器块正在侦听端口 80 和 443 - 您应该将 listen 80 移动到另一个服务器块。
  • 嗨,理查德,感谢您的帮助。没错,我发现了几个与 443 一起使用的“监听”实例,并且我已经删除了这些服务器块,因为它们无论如何都不是必需的。看来我仍然没有被重定向到该网站的 SSL 版本..
  • 回来我想我明白你的意思了 - 我添加了listen 80; 以使整行server { listen 80; return 301 https://memorisemedicine.com$request_uri; } 仍然没有重定向。我正常的 http 流量应该都通过 80
  • 关于 Nginx 和其他服务器相关软件的问题可能会在 SO 姊妹网站 serverfault.com 上得到更好的答案

标签: ubuntu ssl nginx configuration


【解决方案1】:

在您的 nginx 配置中,我看不到为 ssl 侦听端口 443 的服务器部分。 我将添加我的配置,以便您可以根据自己的需要对其进行编辑:

server {
    listen 80;
    listen [::]:80;

    return 301 https://$server_name$request_uri;

    access_log /var/log/nginx/api.example.com-access.log timed;
    error_log /var/log/nginx/api.example.com-error.log;

    root /var/www/example/html/public;

    server_name api.example.com;

    location / {
        index index.html index.php;
        try_files $uri $uri/ @php;
     }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME /var/www/example/html/public/index.php;
        include fastcgi_params;
    }

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

}

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

    access_log /var/log/nginx/api.example.com-access.log timed;
    error_log /var/log/nginx/api.example.com-error.log;

    root /var/www/example/html/public;

    server_name api.example.com;

    include snippets/ssl-api.example.com.conf; 
    include snippets/ssl-params.conf;

    location / {
        index index.html index.php;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME /var/www/example/html/public/index.php;
        include fastcgi_params;
    }

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

}

我的包括 sn-ps/ssl-api.example.com.conf;包括来自 certbotfullchainprivate

【讨论】:

    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 2014-10-11
    • 2018-08-18
    • 2017-02-25
    • 1970-01-01
    • 2014-05-08
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多