【问题标题】:How to configure Nginx to serve https only如何将 Nginx 配置为仅服务于 https
【发布时间】:2018-05-08 12:53:51
【问题描述】:

我是网络服务器领域的新手,我不想让我的网站仅提供 https 服务(适用于 IPV4 和 IPV6),所以我实施了以下步骤,

  1. 安装letsencrypt。
  2. 使用 Nginx 插件安装 certbot。
  3. 使用命令创建证书,

sudo certbot --nginx certonly -d maarath.com -d www.maarath.com

4.在 etc/nginx/site-available/main 中手动配置我的站点配置文件,如下所示,

server {
        listen 80  ;
        listen [::]:80  ;
        root /var/www/main/;
        index index.php index.html index.htm;
        # Make site accessible from http://localhost/
        server_name maarath.com www.maarath.com;
        location / {
                try_files $uri $uri/ =404;
        }

# HTTPS

    listen              443 ssl;
    server_name       maarath.com  www.maarath.com;
    ssl_certificate     /etc/letsencrypt/live/maarath.com/cert.pem;
    ssl_certificate_key /etc/letsencrypt/live/maarath.com/privkey.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;




        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        #deny access to .htaccess files, if Apache's document root
        #concurs with nginx's one
        location ~ /\.ht {

        }
}
  1. 运行命令 nginx -t 没有问题。
  2. 重启 nginx。

问题是我的网站在完成上述所有步骤后仍然不安全,是我遗漏了什么还是做错了什么?任何帮助将不胜感激。

【问题讨论】:

  • 也许你可以将80端口重定向到443

标签: nginx lets-encrypt


【解决方案1】:

拳头,我相信你的配置缺少# HTTPS 正下方的第二个server {

为了做到这一点,您的网站https://maarath.com 会引发 SSL 错误?因为从我的角度来看,它工作得很好。或者你的意思是http没有被重定向到https

如果是这样添加

return 301 https://maarath.com$request_uri;

到您的第一个服务器块。正上方

server_name ...

这应该会自动将所有请求从http 重定向到https

【讨论】:

  • 虽然这种方法可以解决问题,但它会使您的服务器容易出现已知的开放重定向漏洞。更多信息请阅读:netsparker.com/blog/web-security/…更好的解决方案是在应用层解决这个问题。
  • @MajidJafari 这不是真的。 “开放重定向漏洞”是关于未经验证的用户提供的链接(基于应用层的重定向服务)。此问题与 Webserver 无关。如果您尝试按照您的建议在应用程序层上处理它,那将是一个问题。这将是致命的,因为它很容易被利用。
  • 您能详细说明一下吗?这里有什么不同? Web 服务器层重定向如何保护开放重定向?
  • @MajidJafari 它不会“保护”任何东西。它只是容易 受到开放重定向漏洞的影响。文章提到了基于应用层的重定向服务,例如/index.php?redirect=...,当然不是同页网络服务器重定向(HTTP 3XX)。在指责我的回答易受攻击/不安全之前,请仔细阅读文章。
【解决方案2】:

正如 NullDev 提到的,我只是添加新的工作配置文件希望对其他人有所帮助。

server {
    listen 80 ;
    listen [::]:80;
        server_name maarath.com www.maarath.com;

    # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
    return 301 https://$host$request_uri;
}


server {
# HTTPS

    listen              443 ssl;

        listen [::]:443 ssl;
        root /var/www/main/ ;
        index index.php index.html index.htm;
    server_name       maarath.com  www.maarath.com;

    ssl_certificate     /etc/letsencrypt/live/maarath.com/cert.pem;
    ssl_certificate_key /etc/letsencrypt/live/maarath.com/privkey.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

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




        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        #deny access to .htaccess files, if Apache's document root
        #concurs with nginx's one
        location ~ /\.ht {
                deny all;
        }
}

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 2018-01-04
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    相关资源
    最近更新 更多