Nginx设置让我们为端口80和8000加密SSL

时间:2019-08-28 14:51:41

标签: nginx devops digital-ocean

我正在使用 让Encrypt Certbot 生成免费的SSL,一切正常。

我将port 80设置为前端,将port 8000设置为后端。

问题是:

SSL仅适用于PORT 80,而她是我的NGinx配置文件代码。

前端端口80代码:

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

    root /var/www/html/front-end;

    index index.html index.htm index.php;

    server_name tradekeyegypt.tk www.tradekeyegypt.tk;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/tradekeyegypt.tk/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/tradekeyegypt.tk/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

后端8000端口代码:

server {

    listen 8000 default_server;
    listen [::]:8000 default_server;


    root /var/www/html/back-end/public;

    index index.html index.htm index.php;

    location / {
            try_files $uri $uri/ /index.php$is_args$args;
    }

 location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

}

2 个答案:

答案 0 :(得分:0)

在您的后端配置文件上执行此操作

server {

listen 80;
listen [::]:8000 80;


root /var/www/html/back-end/public;

index index.html index.htm index.php;

server_name subdomain.tradekeyegypt.tk www.subdomain.tradekeyegypt.tk;



location / {
        try_files $uri $uri/ /index.php$is_args$args;
}

  location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

 }

此运行服务nginx重新启动后

在创建后端配置文件that之后。在digitalocean后端的服务器中为该子域添加名称记录。

之后,如果您可以在浏览器中打开子域网址。对于certbot运行

sudo certbot --nginx

现在certbot还将读取子域

答案 1 :(得分:0)

SSL在端口443上工作,而不在端口80上工作,端口80处理HTTP请求(而不是HTTPS)。

此外,certbot不支持不同于443的端口,这意味着您应该为客户端和服务器使用相同的端口(443)。

在这种情况下,您可以按路径/api/server区分客户端和服务器之间的请求。

您应将服务器配置更改为以下内容:

location /server/ {
            rewrite ^/api/(.*) /$1 break;
            proxy_pass https://localhost:8000;
             ...
    }

记住要更改客户端中的代码,以将请求发送到新服务器地址。

希望有帮助。