0

I am trying to use nginx with my nodejs site, but i am having issues with the nginx configuration. Initially i couldn't get my css or images to display but i fixed that by adding:

location /images { root PATH_TO_MY_PROJECT_IMAGES_FOLDER }

However, i am having issues when it comes to visiting other pages on the site. I have a login system and when the user logs in the site takes them to "mydomainname.com/home", but when this happens i get a 404 error. In my nodejs code i send the user to home page by:

//should be logged in so show main search page
    app.get('/home', function(req, res) {
        if (req.session.user == null){
            // if user is not logged-in redirect back to login page //
            res.redirect('/');
        }   else{
            res.sendFile(path.join(__dirname + '/FrontEnd/home.html'));
        }
    });

My project is structure as follows:

ProjectNameFolder:
1. public
  1.1 FrontEnd
    1.1.1 login.html
    1.1.2 home.html
  1.2 Lots of other javascript files, some for front-end some for server

If i connect directly to my server via typing it's ip into the browser everything works perfectly, but i'm guessing that's bypassing nginx.

Nginx Config:

location /css {
  root /home/adam/NodeProject/public/FrontEnd/;
}
location /images {
  root /home/adam/NodeProject/public/FrontEnd/;
}
location / {
  try_files $uri $uri/ = 404;
  proxy_pass http://localhost:8888;
  .... other proxy stuff, like setting headers
}

Apologies, if this is a bit long winded, i have tried to provide as much clarity as i can.

Edit - Server Blocks as requested:

Server {
listen 80;
listen [::]:80;
server_name mydomain.com www.mydomain.com
return 301 https://$host$request_uri;
}

server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
root /home/adam/NodeProject/public/;
index index.php index.html index.jpg
server_name mydomain.com www.mydomain.com;
ssl_certifiate /home/adam/SSL/public.crt;
ssl_certificate_key /home/adam/SSL/mydomain.com.key;
        location ~ \.js {
                add_header Content-Type application/x-javascript;
        }

        location ~ \.(css|png|jpg|jpeg|gif|ico|html|woff|woff2|ttf|svg|eot|otf)$ {
                add_header "Access-Control-Allow-Origin" "*";
                expires 1M;
                access_log off;
                add_header Cache-Control "public";
        }

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                #try_files $uri $uri/ /FrontEnd/ =404;
                #try_files $uri
                #       /$server_name$uri
                #       /shared$uri;

                index login.html home.html;

                try_files $uri $uri/ =404;

                proxy_pass http://MY_SERVER_IP:8888;
                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;
        }

        location ~ \.js {
                add_header Content-Type application/x-javascript;
        }
}
user7856951
  • 101
  • 2
  • If it's succeeding when you connect to the IP address on nginx port (i.e, not on port 8888), then chances are that the problem is not with your proxy/location config, but with the listener/server config. May help in that case to post the "server" blocks of your config. – ernest_k May 27 '17 at 15:23
  • Ok, i have added my server blocks – user7856951 May 29 '17 at 09:05

0 Answers0