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



我正在使用Lets 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;
}
}

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

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重新启动

制作后端配置文件后.在数字海洋后端的服务器中添加该子域的名称记录。

如果在此之后可以在浏览器上打开子域 url.对于 certbot 运行

sudo certbot --nginx

现在 certbot 还将读取子域

SSL 适用于端口 443,而不是端口 80,后者处理 HTTP 请求(而不是 HTTPS(。

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

在这种情况下,您可以通过路径来区分客户端和服务器之间的请求,例如/api/server.

您应该将服务器配置更改为如下所示的内容:

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

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

希望有帮助。

最新更新