简体   繁体   English

我无法通过Nginx提供静态服务

[英]I can't serve static by nginx

I have the following nodejs structure which is resides in /home/ubuntu/project directory: 我有以下nodejs结构,它位于/ home / ubuntu / project目录中:

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

I'm trying to serve static assets by nginx, so I wrote the following location: 我试图通过nginx提供静态资产,所以我写了以下位置:

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;
    }
}

But when I try to open up my site in a browser I get failed status on all requested assets. 但是,当我尝试在浏览器中打开网站时,所有请求的资产均显示失败状态。 Whet's the problem? 这是什么问题?

EDIT: My route to css for example is: 编辑:例如,我到CSS的路线是:

http://www.myapp.com/css/styles.css

Well, 好,

Add a / to the root path. 在根路径上添加/

root /usr/share/nginx/www;

should be 应该

root /usr/share/nginx/www/;

Use an alias for the assets like: 对资产使用别名 ,例如:

alias /home/ubuntu/project/site/; (again, add the last /)

These is a mess for me: 这些对我来说是一团糟:

location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico|home/|html|xml)

You should check these http://wiki.nginx.org/NginxHttpCoreModule#location 您应该检查这些http://wiki.nginx.org/NginxHttpCoreModule#location

I dont see these folders images/, javascript/, stylesheets/, flash/, media/, static/ and home/ in your sitemap. 我在您的站点地图中没有看到这些文件夹images/, javascript/, stylesheets/, flash/, media/, static/ and home/

And these both |html|xml are looking for the route /html or /xml not the .html or .xml files. 而且这两个|html|xml都在寻找路由/html/xml而不是.html.xml文件。

Then try: 然后尝试:

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;     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM