dagy
By:
dagy

Help with Nginx Vhost ( Server Blocks) that work intermittently

November 27, 2014 4.1k views

Hi,
Im on a Ubuntu 14.04 droplet with Lemp. I have 4 domains pointing to the server but only the default vhost works correctly. The other vhost work for a minute or 2 and then hang and then they work again and that goes on and on.

1 domain was for larval development
2 for blogs on wordpress
1 for playing with new frameworks

I've follow mostly DO tutorial but I used other sources too.
I've checked the nginx access and error logs with nothing to explained this behaviour.

Im new to nginx I though had understood it but now is not working as it should.

Any ideas on what could be the cause of the problems and what should I do to fix them.

*in /etc/nginx/
*

My nginx.conf -- mostly intact

user www-data;
worker_processes 2;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30s;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 80;
    # server_name_in_redirect off;

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

    #->index files
    index index.php index.html index.htm;


    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;


    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;

    ##
    # 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 /usr;
    #passenger_ruby /usr/bin/ruby;

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

my drop.conf file -> I read was a good practice

location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }  
    location ~ /\.          { access_log off; log_not_found off; deny all; }
    location ~ ~$           { access_log off; log_not_found off; deny all; }

My php.conf file -> to make available php to the vhost

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        }

my wp.conf file -> for passing it to the domains wp host

# WordPress single blog rules.

location / {
    try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
}


# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;

*in /etc/nginx/sites-available/
*

the default vhost conf this the only one that works correctly you can see it on ambumec.com

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location /pma {
            auth_basic "Admin Login";
            auth_basic_user_file /etc/nginx/pma_pass;
    }

    location /status {
     access_log off;
     allow 127.0.0.1;
     allow myip;
     deny all;
     include fastcgi_params;
     fastcgi_pass unix:/var/run/php5-fpm.sock;

}

    location /ping {
     access_log off;
     allow 127.0.0.1;
     allow 186.115.157.106;
     deny all;
     include fastcgi_params;
     fastcgi_pass unix:/var/run/php5-fpm.sock;

}

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #   root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    try_files $uri =404;
    #    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
    #    fastcgi_index index.php;
    #    include fastcgi_params;
    #    }


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

wordpres blog conf file -> this works intermittently but mostly doesn't work

server {
    listen 80;
    listen [::]:80;

    server_name www.davidgarcia.xyz davidgarcia.xyz;

    root /var/www/davidgarcia.xyz/html;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;

    }
    include drop.conf;
    include wp.conf;
    include php.conf;

}

laravel conf file -> this works intermittently but mostly doesn't work

server {
    listen 80;
    listen [::]:80;
    server_name www.eureka.link eureka.link;

    root /var/www/laravel/eureka.link/public;
    index index.php index.html index.htm;


    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;

    }
    include php.conf;
    include drop.conf;
    include laravel.conf;
}

Thank you very much for the assistance you could provide :)

1 comment
  • I have only manage to make the laravel's vhost work.
    The wordpress vhost keep working intermittently and Im running out of ideas. The conf has changed.
    I have destroyed the old droplet and tried everything again for scratch four times and having been able to figure out what is going on with wordpress.

    Again wordpress vhost's are the ones who work intermittently now. My current conf for the wordpress site is:

    • domain (davidgarcia.xyz) bought in hover.com where I assigned digitalocean's nameservers : ns1.digitalocean.com | ns2.digitalocean.com | ns3.digitalocean.com

    -DNS settings set in DO control panel : - A: @ to 104.131.125.64 , CNAME: www to davidgarcia.xyz, NS: ns1.digitalocean.com | ns2.digitalocean.com | ns3.digitalocean.com.

    For the eureka.link domain I did the same.

    *in /etc/nginx/

    For more clarity I deleted drop.conf, php.conf and wp.conf and added the appropriate parts to the Vhost.

    My nginx.conf (showing what is not commented out) is pretty intact:

    user www-data;
    worker_processes 2;
    pid /run/nginx.pid;
    
    events {
        worker_connections 1024;
        # multi_accept on;
    }
    
    http {
    
        ##
        # Basic Settings
        ##
    
        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;
    
        ##
        # Logging Settings
        ##
    
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
    
        ##
        # Gzip Settings
        ##
    
        gzip on;
        gzip_disable "msie6";
    
        ##
        # Virtual Host Configs
        ##
    
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }
    
    

    *in /etc/nginx/sites-available/

    My laravel conf that is working :)

    server {
       server_name eureka.link;
         return 301 $scheme://www.eureka.link$request_uri; 
    }
    
    server {
      listen 80 default_server;
       listen [::]:80 default_server ipv6only=on;
        server_name www.eureka.link;
        root /var/www/laravel/eureka.link/public;
        index index.php index.html index.htm;
    
        location / {
                    try_files $uri $uri/ index.php?$query_string;
            }
    
        location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
    location /status {
         access_log off;
         allow 127.0.0.1;
         allow myip;
         deny all;
    
         include fastcgi_params;
         fastcgi_pass unix:/var/run/php5-fpm.sock;
    
    }
    
        location /ping {
         access_log off;
         allow 127.0.0.1;
         allow 186.115.157.106;
         deny all;
         include fastcgi_params;
         fastcgi_pass unix:/var/run/php5-fpm.sock;
    
    }
    
    }
    

    My wordpress conf ( the one that is driving me crazy).... :

    #server {
    #   server_name www.davidgarcia.xyz;
    #   return 301 $scheme://davidgarcia.xyz$request_uri; 
    #}
     // this is very strange but when I uncomment the lines above the vhost completely crashes,
     on the web browser says that there is a redirect loop but there is nothing in the log files, 
    it works with the laravel vhost but here I just have commented it out 
    
    server {
            listen 80;
            listen [::]:80;
    
            root /var/www/davidgarcia.xyz/html;
            index index.php index.html index.htm;
            server_name davidgarcia.xyz www.davidgarcia.xyz;
    
            error_page 404 /404.html;
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                    root /usr/share/nginx/html;
    
            }
    
    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    
    # Directives to send expires headers and turn off 404 error logging.
    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
           access_log off; log_not_found off; expires max;
    }
    
    location = /robots.txt  { access_log off; log_not_found off; }
            location = /favicon.ico { access_log off; log_not_found off; }  
            location ~ /\.          { access_log off; log_not_found off; deny all; }
            location ~ ~$           { access_log off; log_not_found off; deny all; }
    
        location ~ \.php$ {
                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
        location ~* ^/wp-content/uploads/.*\.php$ {
        return 403;
    }
    
    }
    

    What I have experience is :

    The problem is really strange it doesn't show in any log so far.. the site could work for a couple of minutes and then hang for an hour or more.

    Some times when I do a ping to davidgarcia.xyz gives me a response and some times it says that could not resolve the host and most of the time www.davidgarcia.xyz can't be resolved.

    For a while I though that it was a permission problem but now im not so sure... Im running out of ideas.

1 Answer

@dagy: I'm not sure about some of the issues that you are having, but we can try to solve them one at a time.

In terms of your redirect loop for www.davidgarcia.xyz, I'm not sure, but I would try to change your www entry in your DNS from a CNAME record, to a separate A record. The redirect may be happening in DNS instead of at the web server level. So A: www to 104.131.125.64. If this doesn't fix the redirect issue (once the DNS has refreshed), we can rule that out as the culprit.

If your pings aren't working right, it may likely be a DNS issue. Pings use the ICMP protocol instead of TCP, so they aren't really handled by Nginx. I would focus your efforts on making sure your server is consistently pingable on your domain names before attempting changes to your Nginx configuration.

Have another answer? Share your knowledge.