在ubuntu上设置nginx和节点应用程序
这是情况 -
- node express app1(后端应用程序 - REST API服务器) - 在端口8888上运行 - 一堆POST路由(auth,api,下载,上传..) - 与mysql服务器通信 - 由开发人员#1维护
- 在端口80上运行的节点(webpack,react)app2(前端应用程序) - 由开发人员#2维护
本周我们设置了nginx和HTTPS(通过Let的加密),这意味着当用户访问www.oursite时 - 它被重定向到具有漂亮绿色锁和安全站点的https站点。到现在为止还挺好。但是,当用户输入她的电子邮件时,app2应该调用app1,并且应该发送带有注册令牌的电子邮件。这在我们尝试设置nginx之前运行良好 - 并在两个应用程序上使用http。现在这个电话没有发生。这是我们的nginx设置:
server {
listen 80;
listen [::]:80;
root /var/www/oursite/html;
index index.html index.htm index.nginx-debian.html;
server_name oursite www.oursite;
location / {
try_files $uri $uri/ =404;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/oursite/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/oursite/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
if ($scheme != "https") {
return 301 https://$host$request_uri;
} # managed by Certbot
# Redirect non-https traffic to https
# if ($scheme != "https") {
# return 301 https://$host$request_uri;
# } # managed by Certbot
}
我的问题(开发人员#1) - 我应该将我的应用程序中的所有路由(后端应用程序 - app1 - 后端)替换为https而不是http吗?来自https网站的Apparently Chrome is not calling http request(就我们的情况而言),我应该如何设置nginx呢?我相信我应该在nginx设置中添加另一个位置部分。如果还有其他建议,请告诉我们。
回答如下:每个端口必须获得自己的服务器{}块,因此端口80获取一个,端口443获得另一个端口,每个端口都有一个listen指令到相应的端口...在上面你有两个端口在同一个服务器{}块
如果应用程序暴露在互联网上,他们应该使用https,如果不是他们的传入流量使用http是端口80块必须被重定向发送流量到443 ...所以你的nginx配置文件看起来类似于
server {
listen 80 ;
listen [::]:80 ;
server_name example, www.example;
# Redirect all HTTP requests to HTTPS
rewrite ^/(.*) https://example/$1 permanent;
}
server {
listen 443 ssl ;
listen [::]:443 ssl ;
server_name example;
ssl_certificate /etc/letsencrypt/live/oursite/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/oursite/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
location / {
# direct traffic to my server on its host IP and port
proxy_pass http://172.19.0.5:3000/; # notice http not https
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}
在你的情况下,看起来app1没有暴露在互联网上,因此不会出现在你的nginx配置中...它只接收来自app2下游的http流量...所有上述配置仅适用于你的app2 ...传入的互联网流量使用安全的https端口443(或端口80,它被重定向到443)接收所谓的TLS终止...由nginx TLS终止从互联网屏蔽的应用接收http流量而不是https这是好的,因为应用程序端口不可见互联网......所以你的app2得到的http流量不是https