node.js - 如果 url 中缺少尾部斜杠,nginx 不会将请求传递给节点服务器
问题描述
我已经用 wordpress 设置了 nginx,它工作正常。现在我创建了一个反应应用程序,它在端口 3000 中运行。如果某个位置匹配,我希望我的 nginx 服务器将请求传递给反应服务器。下面是带有 wordpress 和 react 应用程序的 nginx 配置。
listen 80;
server_name aaroogya.org;
return 301 https://aaroogya.org$request_uri;
}
server {
# listen 80;
root /var/www/wordpress;
index index.php index.html index.htm index.nginx-debian.html;
server_name aaroogya.org www.aaroogya.org;
#location = /favicon.ico { log_not_found off; access_log off; }
#location = /robots.txt { log_not_found off; access_log off; allow all; }
#server_name testbed2.covidhelp.in;
location /covidhelp{
#root /var/www/;
# index index.html;
add_header Access-Control-Allow-Origin http://127.0.0.1:3000/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000/ ;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/aaroogya.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/aaroogya.org/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
}
当我访问https://www.aaroogya.org/covidhelp/
它将请求重定向到反应服务器,但是当我尝试加载所有静态文件(如 bundle.js)时,它无法正常工作。例如
https://www.aaroogya.org/covidhelp/static/js/main.chunk.js
——不工作的例子
https://www.aaroogya.org/covidhelp/static/js/main.chunk.js/
——添加了一个斜杠,它的工作正常。
解决方案
我已经通过 2 个步骤解决了这个问题。
- 查看
/var/log/nginx/error.log
connect() failed(111: Connection refused) while connecting to upstream, client: * .*.*.*, server: * .*.*.*, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "*.*.*.*"
127.0.0.1:8000
即使我127.0.0.1:3000
在 nginx conf 文件中将上游设置为上游,上游仍然存在。
- 将服务器 127.0.0.1:8000 替换为 server
127.0.0.1:3000
in/etc/nginx/conf.d/virtual.conf
并重新启动 nginx。
以下:
server {
listen 80;
server_name SERVER_IP_ADDRESS;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
然后:
sudo /etc/init.d/nginx restart
最后,它没有 502 错误。
推荐阅读
- ssl - phpmyadmin 配置期间的 SSL 警告
- angular - 如何同时检测查询参数和片段的变化(或至少以明确定义的顺序)?
- azure - az devops login 无法使用密钥环存储 PAT;回退到文件存储
- spring - Spring Security 中@PreAuthorize 和@PostAuthorize 有什么区别
- python-3.6 - Python 3.6 JSON 字符串到对象的转换
- c# - 如何用列表填充选择器
- string - '格式字符串只能是“G”、“g”、“X”、“x”、“F”、“f”、“D”或“d”。
- python - 如何使用带有 Python 可执行文件的 Mac finder 导入图像
- delphi - Delphi,克隆一个包含孩子的 3DObject
- scala - 重载方法值 Sin 有替代方案