1

I'm trying to serve static files(HTMLs, JS and CSS) from a Google compute engine instance running Debian 8. I used git to clone the web app to the folder /var/www/prod/app with command

sudo git clone <my repo>

I made a conf file in sites-enabled folder app.conf. Here's the content

server {
    listen 80;
    root /var/www/prod/app;
    location / {
    }
}

Isn't this config enough to make my machine serve the files when I type my machine's ip address/path ?

I see the welcome to nginx page when I type my instance's ip address in browser. But none of the files in prod/app are served. I get 404 for all the paths I try. Could someone help me configure this.

Shabin Muhammed
  • 121
  • 1
  • 8
  • Try adding a "server_name example.com" and restarting Nginx. I'm not sure you even need that location. If that doesn't work please edit your question to include the full Nginx config, all site configurations, a curl (showing command line and the start of the output), and the matching Nginx access/error log line (not the whole log). I suspect someone else will spot the problem quickly though. – Tim Apr 19 '17 at 03:00
  • Its a fresh installation of nginx, other than my conf file, there are only default ones in the system. Its a newly created Debian instance. Only installatoins are Git and Nginx. – Shabin Muhammed Apr 19 '17 at 06:51
  • Disable default config – Alexey Ten Apr 19 '17 at 08:00
  • "The default config" could be many things depending on the distribution. That is why it is important to have the full configuration here in order to solve the issue. – Tero Kilkanen Apr 19 '17 at 09:10
  • Hint: Please check if you have exposed your .git folder with your website (www.example.com/.git). The way you described let me assume that. – Jens Bradler Apr 20 '17 at 03:20
  • I figured it out. The `default` config in sites-enabled forlder had a server block that would override my config. Commented it out and things are working as expected. Thanks a lot guys. – Shabin Muhammed May 01 '17 at 08:38
  • Consider posting self-answer with the solution so that other user's with the similar issue can benefit from it. – Faizan May 01 '17 at 19:14

2 Answers2

2

You need to add try_files inside location and make it the default server:

server {
    listen 80 default_server;

    root /var/www/prod/app;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

Then you need to restart the nginx service.

Luis Herranz
  • 121
  • 1
1

I figured it out. Nginx has a default config file (without the extension ".conf") in the following path

/etc/nginx/sites-enable/default

The default config has a server block for default_server that serves the folder /var/www/html. This would override any config I made for serving static files without a server_name field.

I commented out these lines and things are working as expected.

Thanks to Alexy Ten and Tero Kilkaken for pointing me in the right direction.

Shabin Muhammed
  • 121
  • 1
  • 8