将Nginx配置为仅允许https流量

时间:2018-06-26 13:36:43

标签: linux nginx digital-ocean vps

我对linux环境超级陌生,并尝试将vps服务器配置为仅允许https请求。我已经阅读了nginx文档,并尝试了各种重写和return语句,更改服务器块等。但是到目前为止,我已经实现了,站点在http和https上都具有以下配置。

我想要实现的是将这个子域admin.example.com配置为仅处理https请求。

我正在以下位置编辑配置:/ etc / nginx / sites-available / default

server {
    listen 80;

    server_name admin.example.com;


    #return 301 https://admin.example.com$request_uri;


    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/admin.byrides.com/fullchain.pem; # man                                                                                                                                                             aged by Certbot
    ssl_certificate_key /etc/letsencrypt/live/admin.byrides.com/privkey.pem; # m                                                                                                                                                             anaged by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

 }

1 个答案:

答案 0 :(得分:2)

您将需要设置两个服务器指令,一个用于端口80,将流量重定向到端口443。

server {
    listen 80;
    server_name admin.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    ssl on;

    ssl_certificate /etc/letsencrypt/live/admin.byrides.com/fullchain.pem; # man                                                                                                                                                           aged by Certbot
    ssl_certificate_key /etc/letsencrypt/live/admin.byrides.com/privkey.pem; # m                                                                                                                                                             anaged by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
    }
}