如何在Nginx上渲染Sails css和js文件

时间:2016-01-28 19:29:50

标签: javascript css nginx sails.js

我在Nginx上运行我的Sails应用程序,但它既不会渲染我的CSS文件也不会收听我的JS文件。

我的nginx / sites-available / mywebpage.conf:

listen 80;

server_name myservername.com;

root /path/to/page;

location / {
    proxy_pass http://localhost:1337;
    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;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

location ~* \.(img|js|css)$ {
    root /assets;
    expires 7d;
    access_log off;
}

这是我的Sails布局:

<!--STYLES-->
<link rel="stylesheet" href="/styles/bootstrap-theme.css">
<link rel="stylesheet" href="/styles/bootstrap.css">
<link rel="stylesheet" href="/styles/importer.css">
<link rel="stylesheet" href="/styles/style.css">
<!--STYLES END-->

<!--SCRIPTS-->
<script src="/js/dependencies/sails.io.js"></script>
<script src="/js/jquery-1.11.3.min.js"></script>
<!--SCRIPTS END-->

Nginx日志:

2016/01/28 14:50:41 [error] 8319#0: *7 open() "myservername/styles/bootstrap.css" failed (2: No such file or directory), client: 127.0.0.1, server: myservername.com, request: "GET /path/to/page/styles/bootstrap.css HTTP/1.1", host: "myservername.com", referrer: "http://myservername.com/sailsroute"

每个CSS和JS文件都会显示此错误。

有人知道如何渲染这些文件吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

我已经在Nginx上成功运行了风帆应用,

在我启用的Nginx网站上配置:

    # Load balancer configuration                                                                                                                                                                                                                  
    upstream example.com {
        # Directs to the process with least number of connections.
        least_conn;

        `enter code here`# One failed response will take a server out of circulation for 20 seconds.
        server 127.0.0.1:10080 fail_timeout=20s;
        server 127.0.0.1:10081 fail_timeout=20s;
        server 127.0.0.1:10082 fail_timeout=20s;
        server 127.0.0.1:10083 fail_timeout=20s;

        keepalive 64; 
    }

    # WebSocket "upgrade" method
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    # Server settings
    server {
        (...)

        #Set server root
        root /usr/share/nginx/html/example.com/.tmp/public;

        (...)

        # pass the request to the node.js server with the correct headers
        location / {
            # Setting proxy headers
            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;

            #Set proxy
            proxy_pass http://example.com;
            proxy_redirect off;
            proxy_http_version 1.1;

            # WebSocket headers
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;      
        }

        (...)
    }
    (...)

此配置似乎工作正常。

我希望我能以某种方式帮助。

再见