Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve static content through subdomain in nginx

Tags:

nginx

I have some slate docs as website and would like to serve them on the internal server, through a subdomain as follows: internal-docs.mysite.com. For the record, accessing mysite.com shows the "nginx is running propertly" page.

I've created a config file with following path and name: /etc/nginx/sites-available/internal-docs.mysite.com:

server {
    listen 80;
    server_name internal-docs.mysite.com;

    root /var/www/docs-internal;
    index index.html;

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

    error_page 404 /404.html;
}

And of course, I've put the files in /var/www/docs-internal. And then I made a symlink to the uppershown config file in the /etc/nginx/sites-enabled dir:

internal-docs.mysite.com -> ../sites-available/internal-docs.mysite.com

Then I reload nginx -s reload but "this site can't be reached" error is what I get when accessing the URL.

The setup and configuration look correct to me (according to the guidelines I've followed), so that's why I'm in a dead end, sort of...

like image 241
Milkncookiez Avatar asked Mar 07 '17 09:03

Milkncookiez


2 Answers

It seems you forgot the Listen directive. Try the following:

server {
    listen 80;
    server_name internal-docs.mysite.com;

    root /var/www/docs-internal;
    index index.html;

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

    error_page 404 /404.html;
}

If that does not work, check:

  1. That Nginx user has read permission to the site content. For example if your Nginx user is www and you have root access, do the following:

    # su www
    $ cat /var/www/docs-internal/index.html
    

    If that fails, ensure the location has correct ownership and permissions. Note that for a user to be able to browser a directory, that directory must have the execute bit for that user or user group.

  2. That Nginx user has read permission on file ../sites-available/internal-docs.mysite.com. For example if your Nginx user is www and you have root access, do the following:

    # su www
    $ cat /etc/nginx/sites-available/internal-docs.mysite.com
    

    If that fails, ensure that the config files have correct ownership. Note: normally Nginx master process is run by root, and that process spawns sub-processes run as Nginx user, so permissions on config files are unlikely to be the problem.

  3. That maybe your config file name should end with ".conf" (on my server I have the following line: include conf.d/*.conf; so it will NOT load any conf file ending with ".com".

  4. That Nginx tries to load files in ../sites-available/ in its main config file. Maybe it does not and looks instead in the conf.d directory (the default).

  5. That you can do a ping and nslookup on the subdomain. If you cannot, then you have to fix that first (DNS, firewall...).

like image 84
Gabriel Hautclocq Avatar answered Sep 21 '22 04:09

Gabriel Hautclocq


For the sake of others - the configuration I wrote was correct, and my problem was in 2 things:

  1. I had to remove the listen 80 directive, since there is another configuration file already, that specifies that nginx should listen on port 80. One should not tell nginx twice to listen on the same port, even if it's in two separate configuration files
  2. Permissions on the /var/www/docs-internal folder. Opening a folder requires x (execute) permissions, while opening a file requires r (read) perm. I had to provide the according permissions to all the folders in this hierarchy, so that the content could be open globally (from everyone), which is basically accessing it from the browser.
like image 24
Milkncookiez Avatar answered Sep 22 '22 04:09

Milkncookiez