Nginx(子)于域名设置HTTPS

Posted by     "" on Monday, June 4, 2018

服务器端口配置

如果是子域名,需要在服务器的出方向和入方向都开放对应的端口,主域名入方向 80 端口就可以。

安装Certbot

安装过程参照Certbot,这里不多赘述

编辑 .conf 文件

我的 Ubuntu 在 /etc/nginx/ 下会有 nginx.conf 文件,可以在这里编辑,也可以在 conf.d 目录下新建 xxx.conf 文件,因为在 nginx.conf 文件下有这两行代码:

include /etc/nginx/conf.d/*.conf;

所以

vim /etc/nginx/nginx.conf

或者

vim /etc/nginx/conf.d/yourdomain.conf

输入以下内容:

server{
        listen       80;
        server_name yourdomain.com;
        # 或者你的子域名 server_name sub.yourdomain.com;
        location / {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;

                #proxy_pass http://127.0.0.1/你子域名对应的开放端口;
                index index.html;
       				 }
        }

如果是子域名则输入:

server{
        listen       80;
        server_name sub.yourdomain.com;
        location / {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;

                proxy_pass http://127.0.0.1/你子域名对应的开放端口;
                index index.html;
       				 }
        }

注意如果是编辑 nginx.conf , 需要写在 http{} 内。然后执行:

nginx -t -c /etc/nginx/nginx.conf

生成证书

检查你的 Nginx 是否配置正确,再用 Certbot 生成证书:

certbot certonly --nginx

之后会在 /etc/letsencrypt/live/ 下生成你的域名为名的文件夹,里面有 cert.pem, chain.pem, fullchain.pem, private.pem 四个文件

配置证书

现在回到之前编辑的 .conf 文件,修改 listen 端口为 443 ssl 并添加证书路径:

server{
        listen       443 ssl;
        server_name yourdomain.com;

        ssl_certificate  /etc/letsencrypt/live/yourdomain.com/fullchain.pem;#cert文件路径
        ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;#key文件路径
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        location / {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                index index.html;
        		}
        }

对于子域名配置文件:

server{
        listen       443 ssl;
        server_name sub.yourdomain.com;

        ssl_certificate  /etc/letsencrypt/live/sub.yourdomain.com/fullchain.pem;#cert文件路径
        ssl_certificate_key /etc/letsencrypt/live/sub.yourdomain.com/privkey.pem;#key文件路径
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        location / {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_pass http://127.0.0.1/你子域名对应的开放端口;
                index index.html;
        		}
        }

进一步确认配置文件的正确性:

nginx -t -c /etc/nginx/nginx.conf

然后重新加载 Nginx 配置文件并重启:

service nginx reload
service nginx restart

然后就可以 https://yourdomain... 访问了。

参考 Erestu的博客

「真诚赞赏,手留余香」

EagleYing's Blog

真诚赞赏,手留余香

使用微信扫描二维码完成支付