代理静态文件上的nginx Express - 404 [英] proxying nginx Express - 404 on static files
问题描述
当我从我的服务器ip:port浏览网站时,expressjs应用程序的静态文件完美工作,但是当应用程序从nginx服务时,静态文件会给出404。这是我的nginx conf:
Static files of expressjs app perfectly working when I browse the site from my server ip:port, but when the app serves from nginx, static file gives 404 . Here is my nginx conf:
upstream project {
server localhost:6546;
}
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.com_access.log;
error_log /var/log/nginx/example.com_error.log;
location / {
proxy_pass http://project/;
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;
}
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
}
这里是我的expressjs代码静态:
and here is my expressjs code for static:
app.enable('trust proxy');
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
if (app.get('env') === 'production') {
app.set('view cache', true);
}
推荐答案
虽然express.js已经建成在静态文件处理通过一些连接中间件,你永远不应该使用它。 Nginx可以处理静态文件做得更好,可以防止非动态内容的请求阻塞节点进程。这里是一个例子:
Although express.js has built in static file handling through some connect middleware, you should never use it. Nginx can do a much better job of handling static files and can prevent requests for non-dynamic content from clogging node processes. here is an example of doing this:
http {
...
server {
...
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /home/ubuntu/expressapp/public;
access_log off;
expires max;
}
...
}
}
这篇关于代理静态文件上的nginx Express - 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!