None of my configurations is taking effect on Nginx

March 15, 2017 717 views
Nginx Ruby on Rails Ubuntu

I have a dedicated DreamHost Server where I installed a ruby on rails app under /srv directory, I installed nginx with passenger and configured nginx to open my app when I access my domain which is the server name .dreamhost.com. However, it is still showing welcome to nginx page even if I run sudo /etc/init.d/nginx reload or stop or service nginx stop I get no error but nothing changes and I keep getting the welcome page as if the restart or stop didn't take any effect. Also I tried to remove nginx but nothing changed. This is my /etc/nginx/nginx.conf file:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

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;

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # 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/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    passenger_root /home/madrasati/.rvm/gems/ruby-1.8.7-head/gems/passenger-5.1.2;
    passenger_ruby /home/madrasati/.rvm/gems/ruby-1.8.7-head/wrappers/ruby;
#   include /etc/nginx/passenger.conf;

    ##
    # Virtual Host Configs
    ##

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


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

This is etc/nginx/sites-available/myapp file

server {
    listen 80 default_server;
    server_name www.madrasatie.com *.madrasatie.com ds10316.dreamservers.com;
    passenger_enabled on;
    passenger_app_env production;
    root /srv/fedena_app/fedena_cds/public;
}

And this is /home/madrasatie/nginx/madrasatie:

server {
    listen 80 default_server;
    server_name www.madrasatie.com *.madrasatie.com *.208.97.137.194;
    passenger_enabled on;
    passenger_app_env production;
    root /srv/fedena_app/fedena_cds/public;
}



I tried so many things and nothing is taking effect. Please help me solve this.

3 Answers

@raafathamzeh

include /etc/nginx/sites-enabled/*;

The above line tells NGINX to pull all website related configuration files from sites-enabled, so if you aren't symlinking configuration files from sites-available to sites-enabled, then NGINX is loading the first or default configuration that it's shipped with (normally named default).

When you run:

ls /etc/nginx/sites-available

... you should see a file called default or default.conf. This is the actual file. Now when you run:

ls /etc/nginx/sites-enabled

... the file you see there is the symlink (a link to the file in sites-available). You can safely delete that file as it's just meant to be used as an example to get you started.

That being said, configuration will only be loaded from where you tell NGINX in nginx.conf, so if you don't want to bother with symlinks (which can be a pain at times), I'd change this line:

include /etc/nginx/sites-enabled/*;

to

include /etc/nginx/sites-available/*;

... and save nginx.conf.

...

As for your actual configuration for your site/app, all configuration should be stored in one of the directories above (sites-available or siites-enabled) -- if you do what I recommended above, then that would be sites-available. You don't need to store configuration files for NGINX in a public directory (which appears to be the case for the second that you mention).

So if you have two configuration files for two sites, then you should have something such as:

/etc/nginx/sites-available/mysite01.conf
/etc/nginx/sites-available/mysite02.conf

Other than that, both of the configuration files you've posted are marked as default_server. This should only be defined for one configuration file, or not at all, else NGINX is going to throw an error.

Also, I always recommend cleaning up any configuration to prevent potential issues, so below is an exact copy of your NGINX configuration with all the un-needed filler, just to keep things clean.

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

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

    gzip on;
    gzip_disable "msie6";

    passenger_root /home/madrasati/.rvm/gems/ruby-1.8.7-head/gems/passenger-5.1.2;
    passenger_ruby /home/madrasati/.rvm/gems/ruby-1.8.7-head/wrappers/ruby;
    #include /etc/nginx/passenger.conf;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
  • @jtittle Thank you so much for helping me understand how nginx should be configured. However, I applied all what you mentioned but still nothing has changed.

    • Did you reload Nginx after that?

      • Yes of course, but as I mentioned even when I stop nginx the webpage still shows welcome to nginx.

        • Try uninstalling Nginx and see if it still does that

        • try these commands to remove nginx completely

          sudo apt-get purge nginx nginx-common
          

          or

          sudo apt-get remove nginx-core nginx-full nginx-light nginx-extras nginx-naxsi nginx-common
          

          as seen on this stack-overflow answer
          http://askubuntu.com/a/729862

          • [ds10316]$ sudo apt-get remove nginx-core nginx-full nginx-light nginx-extras nginx-naxsi nginx-common
            Reading package lists... Done
            Building dependency tree
            Reading state information... Done
            Package nginx-common is not installed, so not removed
            Package nginx-core is not installed, so not removed
            Package nginx-extras is not installed, so not removed
            Package nginx-full is not installed, so not removed
            Package nginx-light is not installed, so not removed
            Package nginx-naxsi is not installed, so not removed
            The following packages were automatically installed and are no longer required:
              geoip-database libgeoip1
            Use 'apt-get autoremove' to remove them.
            0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
            [ds10316]$ sudo apt-get autoremove nginx-core nginx-full nginx-light nginx-extras nginx-naxsi nginx-common
            Reading package lists... Done
            Building dependency tree
            Reading state information... Done
            Package nginx-common is not installed, so not removed
            Package nginx-core is not installed, so not removed
            Package nginx-extras is not installed, so not removed
            Package nginx-full is not installed, so not removed
            Package nginx-light is not installed, so not removed
            Package nginx-naxsi is not installed, so not removed
            The following packages will be REMOVED:
              geoip-database libgeoip1
            0 upgraded, 0 newly installed, 2 to remove and 1 not upgraded.
            After this operation, 7356 kB disk space will be freed.
            Do you want to continue [Y/n]? y
            (Reading database ... 171408 files and directories currently installed.)
            Removing geoip-database ...
            Removing libgeoip1 ...
            Processing triggers for libc-bin ...
            ldconfig deferred processing now taking place
            [ds10316]$ sudo service nginx status
             * nginx is not running
            [ds10316]$
            
            

            I did the following and still nginx is not removed. Also which nginx returns nothing.

  • @jtittle
    sorry to jump in the topic.

    i was trying to turn off access login of static files for my entire server. so i went to add the following in my nginx global config file inside http block

    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    access_log off;
    log_not_found off;
    }
    

    but it didnt worked. then i added server inside http bock, but still didnt worked. i even tried inside a server block outside of http but it gave me syntax error.

    that only work if i add it in sites-available

    is there a way to set access log off globally?

    • @newbie

      That'd need to be placed inside your server block instead of within the http block. Ideally, you want some level of logging, whether it's a global access and error log, or per site.

      Otherwise you can use:

      access_log off;
      error_log off;
      

      or

      access_log /dev/null;
      error_log /dev/null;
      
      • but i dont want to turn off logging completely. just static files like js, css etc

        • @raafathamzeh

          This would need to be done per server block using what you provided above as the location blocks need to be within the server block:

          server {
              ...
              ...
              ...
          
              location .... {
                  access_log off;
                  log_not_found off;
              }
          }
          
        • @newbie - Please see my response above :-).

          This would need to be done per server block using what you provided above as the location blocks need to be within the server block.

@raafathamzeh

Please run this command to test your configuration first.

service nginx configtest

And you cannot have two server-sections competing for the same domain.
And you cannot use wildcard (the *) like that with IP-addresses.
And you have probably not added your server-section configurations to /etc/nginx/sites-enabled.

Then remember to run service nginx configtest and then service nginx reload after every change, so you test the configurations before rebooting nginx.

By the way, you do know that DigitalOcean is a competing company to DreamHost Servers ?

@raafathamzeh

The apt-get remove ... command won't remove NGINX configuration files -- only packages.

Anything that was added by NGINX -- such as configuration files -- or anything you've added -- also such as configuration files -- won't be removed; that'd be what apt-get purge ... is for.

i.e.

sudo apt-get purge nginx

or

sudo apt-get purge nginx-*

It is important to note, however, if you run purge, it will delete anything you've added. If you need to backup your existing configuration for reference, please do that before running the command.

Once NGINX is removed/purged, you can reinstall it using:

sudo apt-get -y install nginx-full

Once NGINX is installed, you can begin to reconfigure.

Have another answer? Share your knowledge.