geckels1
geckels1

Reputation: 596

Why does nginx think my root directory is /usr/share/nginx and not /var/www/html as my configuration states?

I am new to nginx and trying to get the hang of it. I've been reading the docs, and they say if use the directive root it should tell nginx where to find requests. For example, from my understanding, root /var/www/html should tell nginx to find requests in the directory /var/www/html, but my instance of nginx is not doing that. I am trying to load a file in that directory called test.html, but instead it is trying to look for the file in /usr/share/nginx. Note that this is a pretty fresh install of nginx and I have made few changes to the default config files. I also want to note the path prefix is set to /usr/share/nginx, but my understanding is using the root directive should override that. I am running Ubuntu 18.04 and installed nginx through apt. Let me know if you need any more information. Thanks!

nginx.conf - Please note this file has no uncommented root directives

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##


    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
    }

    server {
      listen 80;

      server_name kramericaindustries.hopto.org;
      rewrite ^/rstudio$ $scheme://$http_host/rstudio/ permanent;

      location /rstudio/ {
        rewrite ^/rstudio/(.*)$ /$1 break;
        proxy_pass http://localhost:8787;
        proxy_redirect http://localhost:8787/ $scheme://$http_host/rstudio/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 20d;
      }

      location /heatmap/ {
        proxy_pass http://127.0.0.1:8050;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

      }

      # location /test/ {
      #   root /home/grant/test;
      #   index index.html;
      # }
    }

    # server {
    #   listen 8050;

    #   server_name kramericaindustries.hopto.org;

    #   location /heatmap/ {
    #     proxy_pass http://127.0.0.1:8050;
    #     proxy_set_header Host $host;
    #     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #   }
    #   location /test/ {
    #   }
    # }

    # server {
    #   location /test {
    #     root /home/grant/www;
    #   }
    # }
}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

/etc/nginx/sites-available/default - Please note this file has been unchanged since I installed it and is where the root directive I'm referring to is.

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;


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

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

EDIT: There is a soft symlink in /etc/nginx/sites-enabled/default which points to /etc/nginx/sites-available/default.

Upvotes: 7

Views: 26281

Answers (1)

geckels1
geckels1

Reputation: 596

OK I've solved the issue by learning something new about nginx. The problem is the server block for port 80 in nginx.conf and /etc/nginx/sites-enabled/default were in conflict which I was unaware of. Though /etc/nginx/sites-enabled/default is listed as the default server (listen 80 default_server), nginx was using the server block in nginx.conf because this server block has the server name directive (server_name kramericaindustries.hopto.org;) which took precedence over the default_server. (Yes, I was using this domain name for testing.) nginx only uses one server block to fulfill the request.

Because the server block in nginx.conf did not specify a root, it used the nginx path prefix by default which is /usr/share/nginx which does not contain test.html. Therefore, the request failed. I added root /var/www/html; to nginx.conf and everything is now working as expected.

Upvotes: 9

Related Questions