gpt4 book ai didi

node.js - 我不能通过 nginx 提供静态服务

转载 作者:行者123 更新时间:2023-12-04 19:21:36 25 4
gpt4 key购买 nike

我有以下 nodejs 结构,它位于/home/ubuntu/project 目录中:

 sever
site
|-css
| |-styles.css
|-img
| |-sprite.png
|-js
|-script.js

我正在尝试通过 nginx 提供静态 Assets ,所以我写了以下位置:
upstream myapp_upstream {
server 127.0.0.1:3000;
keepalive 64;
}

server {
listen 80;

server_name www.myapp.com;

error_page 400 404 500 502 503 504 /50x.html;
location /50x.html {
internal;
root /usr/share/nginx/www;
}

location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico|home/|html|xml) {
root /home/ubuntu/project/site;
access_log off;
expires max;
}

location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://myapp_upstream;
proxy_intercept_errors on;
}
}

但是当我尝试在浏览器中打开我的网站时,我在所有请求的 Assets 上都出现了失败状态。有什么问题?

编辑:
例如,我的 css 路线是:
http://www.myapp.com/css/styles.css

最佳答案

出色地,

添加 /到根路径。

root /usr/share/nginx/www;

应该
root /usr/share/nginx/www/;

使用 alias对于以下 Assets :
alias /home/ubuntu/project/site/; (again, add the last /)

这些对我来说是一团糟:
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico|home/|html|xml)

你应该检查这些 http://wiki.nginx.org/NginxHttpCoreModule#location

我没有看到这些文件夹 images/, javascript/, stylesheets/, flash/, media/, static/ and home/在您的站点地图中。

而这两个 |html|xml正在寻找路线 /html/xml不是 .html.xml文件。

然后尝试:
location ~ ^/(robots.txt|humans.txt) {
alias /home/ubuntu/project/site/;
access_log off;
expires max;
}

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { //add here all the file extensions needed.
alias /home/ubuntu/project/site/;
access_log off;
expires max;
}

关于node.js - 我不能通过 nginx 提供静态服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com