Michi Salazar
Michi Salazar

Reputation: 146

How to render Sails css and js files on Nginx

I am running my Sails app on Nginx, but it doesnt neither render my CSS files nor listen to my JS files.

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

This is my Sails layout:

<!--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 log:

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"

This error is shown with each CSS and JS file.

Does anybody know how to render such files?

Thanks in advance!

Upvotes: 0

Views: 362

Answers (1)

ctimoteo
ctimoteo

Reputation: 11

i have sails app successfully running on Nginx,

on my Nginx sites-enabled config i have:

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

        (...)
    }
    (...)

This config appears to be working fine.

I hope i helped somehow.

Bye

Upvotes: 1

Related Questions