nginx和nodejs 404& 403用于某些文件

时间:2016-03-04 01:27:20

标签: javascript node.js nginx

我安装了NGINX的NodeJS服务器 我构建了一个简单的页面,但有些文件得到404& 403,我不知道为什么 可能是愚蠢的事://

非常感谢:)

这是我的页面:

<!DOCTYPE html>
<html>
  <head>
    <title>Dummy Angular App</title>
    <link rel='stylesheet' href='/resources/stylesheets/style.css' />
    <script src="/angular/angular.js"></script>
    <script src="/angular-route/angular-ui-router.js"></script>
  </head>
  <body>
    <h1>Dummy Angular App</h1>
    <p>No Angular Yet!</p>
    <script src="/resources/javascripts/app.js"></script>
  </body>
</html>

我有默认的NodeJS树:

/myApp
--------->/app
--------->/node_modules
-------------->/angular
-------------->/angular-ui-router
--------->/public
-------------->/images
-------------->/stylesheets
-------------->/javascripts

这是我的nginx配置:

location /angular {
                root /srv/www/testapp/node_modules;
                try_files $uri $uri/  =404;

}

location /angular-route {
                root /srv/www/testapp/node_modules;
                rewrite /angular-route /angular-ui-router/release;
                try_files $uri $uri/ = 404;
}

location /resources/ {
                root /srv/www/testapp/public;
                rewrite_log on;
                error_log /var/log/nginx/localhost.error_log notice;
                rewrite ^/resources/(.*)$ /$1 last;
                try_files $uri $uri/ = 404;
}

location / {
                proxy_pass http://127.0.0.1:3000;
                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;

                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
}

  1. /resources/stylesheets/style.css - 404
  2. /angular/angular.js - 工作
  3. /angular-route/angular-ui-router.js - 禁止使用(与角色相同的权限)
  4. /resources/javascripts/app.js - 404
  5. 这是我的日志(似乎重写有效):

    2016/03/04 03:00:07 [notice] 9334#0: *14 "^/resources/(.*)$" matches "/resources/stylesheets/style.css", client: 10.0.0.3, server: webserver.com, request: "GET /resources/stylesheets/style.css HTTP/1.1", host: "www.webserver.com", referrer: "http://www.webserver.com/"
    2016/03/04 03:00:07 [notice] 9334#0: *14 rewritten data: "/stylesheets/style.css", args: "", client: 10.0.0.3, server: webserver.com, request: "GET /resources/stylesheets/style.css HTTP/1.1", host: "www.webserver.com", referrer: "http://www.webserver.com/"
    2016/03/04 03:00:07 [notice] 9334#0: *18 "^/resources/(.*)$" matches "/resources/javascripts/app.js", client: 10.0.0.3, server: webserver.com, request: "GET /resources/javascripts/app.js HTTP/1.1", host: "www.webserver.com", referrer: "http://www.webserver.com/"
    2016/03/04 03:00:07 [notice] 9334#0: *18 rewritten data: "/javascripts/app.js", args: "", client: 10.0.0.3, server: webserver.com, request: "GET /resources/javascripts/app.js HTTP/1.1", host: "www.webserver.com", referrer: "http://www.webserver.com/"
    2016/03/04 03:00:07 [notice] 9334#0: *16 "^/resources/(.*)$" matches "/resources/javascripts/app.js", client: 10.0.0.3, server: webserver.com, request: "GET /resources/javascripts/app.js HTTP/1.1", host: "www.webserver.com", referrer: "http://www.webserver.com/"
    2016/03/04 03:00:07 [notice] 9334#0: *16 rewritten data: "/javascripts/app.js", args: "", client: 10.0.0.3, server: webserver.com, request: "GET /resources/javascripts/app.js HTTP/1.1", host: "www.webserver.com", referrer: "http://www.webserver.com/"

1 个答案:

答案 0 :(得分:0)

试试这个:

location /angular-route {
    root /srv/www/testapp/node_modules/angular-ui-router/release;
    rewrite ^/angular-route/(.*)$ /$1 break;
    try_files $uri $uri/ = 404;
}

location /resources/ {
    root /srv/www/testapp/public;
    rewrite ^/resources/(.*)$ /$1 break;
    try_files $uri $uri/ = 404;
}