Question

Help with Nginx Vhost ( Server Blocks) that work intermittently

  • Posted on November 27, 2014
  • dagyAsked by dagy

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 :)

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

@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.