【发布时间】:2019-05-07 11:37:30
【问题描述】:
我正在运行 nginx,并且我还有一个带有反应前端的快速后端服务器。我遇到的问题是来自 express 的静态文件。例如,我有一些带有调用 css/style.css 和 js 目录的标题的车把视图文件,这些文件目前在 Chrome 中有效,但在 IE、Edge 或 Safari 中无效。在这些浏览器中,控制台显示 404,并且样式当然不适用。
我从我的车把视图页面调用 style.css,如下所示:
<link rel="stylesheet" href="css/style.css">
应该这样设置,如果我要访问http://sitename.com/css/style.css,我会从 /var/www/sitename.com/html/node/public/css/style.css 位置看到 style.css。这实际上似乎在 Chrome 中有效,但在其他浏览器中无效。
我的快递应用中有此声明
app.use(express.static('public'));
我有一个这样的目录结构:
/var/www/sitename.com/html/node (node express app is running from here)
/var/www/sitename.com/html/node/public (public folder for static files from express)
-> css (folder)
-> js (folder)
我的 nginx 设置如下:
server {
listen 80;
server_name _;
root /var/www/sitename.com/html;
index index.php index.html;
server_name sitename.com www.sitename.com;
location /phpmyadmin {
try_files $uri $uri/ =404;
}
location / {
root /var/www/sitename.com/html/node/public/;
try_files $uri @backend;
}
location @backend {
proxy_pass http://localhost:42134;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
# set max upload size
client_max_body_size 2G;
fastcgi_buffers 64 4K;
access_log /var/log/nginx/http_access.log combined;
error_log /var/log/nginx/http_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$
{
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(htaccess|htpasswd) {
deny all;
}
# set long EXPIRES header on static assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
access_log off;
}
}
【问题讨论】:
-
奇怪,因为您通过 nginx 提供公共文件,所以您应该在 app.js 中注释
app.use(express.static('public'));。您也应该尝试在@backend 服务器中设置索引指令(我认为)... -
在我的 app.js 中注释掉 express.static 行不会导致任何变化。我还测试了在@backend 下添加索引指令,似乎没有任何明显的差异。
标签: node.js express nginx nginx-location