[英]How do I serve static assets (html) to a node server running with nginx?
I have a server running with pm2 that is being served up by nginx.我有一台运行 pm2 的服务器,它由 nginx 提供服务。 I would like to point my react app to the domain, and have the server only respond to requests that the client sends.
我想将我的反应应用程序指向域,并让服务器只响应客户端发送的请求。
currently if you go to jwcuisine.io it gives you a "CANNOT GET /" message, I tried something like this:目前,如果你 go 到 jwcuisine.io 它会给你一个“CANNOT GET /”消息,我试过这样的事情:
location / {
# This would be the directory where your React app's static files are stored at
root /var/www/html/;
try_files $uri /index.html;
}
location /graphql {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:5000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
However, the above ^^^ led to a 500 error from nginx.但是,上面的 ^^^ 导致 nginx 出现 500 错误。
Below is the code I currently have, that is giving the /GET response.下面是我目前拥有的代码,它给出 /GET 响应。 Any direction would be appreciated, I can't find a ton of relevant information pertaining to this.
任何方向将不胜感激,我找不到与此相关的大量相关信息。
server {
server_name jwcuisine.io www.jwcuisine.io;
location / {
proxy_pass http://localhost:4000; #whatever port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/jwcuisine.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/jwcuisine.io/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
}
server {
if ($host = www.jwcuisine.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = jwcuisine.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name jwcuisine.io www.jwcuisine.io;
return 404; # managed by Certbot
}
I am also using similar architecture to host my website front-end and back-end via nginx reverse proxy server.我还使用类似的架构通过 nginx 反向代理服务器托管我的网站前端和后端。 There is an update in your nginx conf after which it will work as expected.
您的 nginx conf 中有一个更新,之后它将按预期工作。 That is you need to add first all subroutes(Ex.
/api
, graphql
) and then you need to add the location for index route /
.也就是说,您需要首先添加所有子路由(例如
/api
, graphql
),然后您需要添加索引路由/
的位置。 In your current conf website will never be able to go to server as /server
route gets matched to index route /
first and it will try to find it in the static folder and never reach the proxy_pass
you have provided in below location.在您当前的 conf 网站中,将永远无法将 go 连接到服务器,因为
/server
路由首先与索引路由匹配/
它将尝试在 static 文件夹中找到它,并且永远不会到达您在以下位置提供的proxy_pass
。
Update nginx.conf:更新 nginx.conf:
server{
listen 80 default_server;
listen [::]:80 default_server;
server_name jwcuisine.io www.jwcuisine.io;
location /graphql {
proxy_pass "http://127.0.0.1:5000";
proxy_read_timeout 5400s;
proxy_send_timeout 5400s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}
location / {
root /var/www/html;
try_files $uri $uri/ /index.html;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.