简体   繁体   English

如何将 static 资产 (html) 提供给运行 nginx 的节点服务器?

[英]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 / .也就是说,您需要首先添加所有子路由(例如/apigraphql ),然后您需要添加索引路由/的位置。 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.

相关问题 如何为我的node-js服务器提供静态文件,这些文件位于应用程序根目录中 - How do I serve static files for my node-js server which are in my application root directory 如何使用Node Express提供静态文件? - How do I serve a static file using Node Express? 如何从作为nodejs服务器的反向代理的nginx服务器提供静态文件? - How do you serve static files from an nginx server acting as a reverse proxy for a nodejs server? 如何使用node-restify在NGINX上正确地为静态站点提供服务? - How to correctly serve a static site on NGINX with node-restify? 使用nginx与流星分开服务静态资产和媒体? - Serve static assets and media separately from meteor with nginx? 将nginx放在nodejs前面以提供静态资产是否合理? - Is it reasonable to put nginx in front of nodejs to serve static assets? 如何通过身份验证提供静态文件? - How do I serve static files with authentication? 如何使用Hapijs正确提供静态资产 - How to properly serve static assets with Hapijs 我如何强制 express 为没有文件扩展名且内容类型为“text/html”的静态 html 文档提供服务 - How do i force express to serve static html documents having no file extentions with 'text/html' content type 如何使用背包管理静态资产 - How do I manage static assets with backpack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM