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

How to render Sails css and js files on Nginx

本文关键字:css Sails js 文件 Nginx      更新时间:2023-09-26

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

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

这是我的帆布局:

<!--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 文件一起显示。

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

提前感谢!

我有帆船应用程序在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;      
        }
        (...)
    }
    (...)

此配置似乎工作正常。

我希望我以某种方式有所帮助。

再见