Skip to content

Instantly share code, notes, and snippets.

@santosoide
Forked from folivares/laravel_nginx.md
Created April 10, 2017 11:11
Show Gist options
  • Save santosoide/b897098edd7b760a71df6c6efb34d187 to your computer and use it in GitHub Desktop.
Save santosoide/b897098edd7b760a71df6c6efb34d187 to your computer and use it in GitHub Desktop.
Nginx configuration for Laravel 5.1

Nginx Server Blocks configuration to run more than one Laravel 5.1 web-app off of a single Linux server

Prerequisites

  • PHP package: php5-fpm php5-mcrypt php5-mysql
  • Laravel 5.1
  • Nginx 1.8

Default Server Block

Nginx Server Blocks are located in /etc/nginx/sites-available. The filename for default block is "default".

server {
	# default server specification
	listen 80 default_server;

	# site accessible from http://domain_or_ip
	server_name domain_or_ip;
	
	# file location	
	root /var/www/html;
	index index.php index.html index.htm;

	# check if a file or directory index file exists, else route it to 404 error page.
	location / {
        	try_files $uri $uri/ =404;
   	 }

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

	# handle execution of PHP files
	# set php5-fpm socket
	# tell NGINX to proxy requests to PHP FPM via the FCGI protocol
	location ~ \.php$ {
        	try_files $uri =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;
    }

}

Other Domains Server Blocks

Other blocks are located in same folder of default block (/etc/nginx/sites-available). Filename for each domain is domain_name.

server {
    listen 80;

    # site accessible from domain_name
    server_name domain_name;

    # file location (note the "public" folder of Laravel)
    root "/var/www/domain_name/public";
    index index.html index.htm index.php;
	
    # charset of "Content-Type" response header field
    charset utf-8;
    
    # set the maximum allowed size of the client request body
    #client_max_body_size 100m;

    # log settings
    access_log off;
    error_log  /var/log/nginx/domain_name-error.log error;

    # turn off access logs and prevents logging 
    # an error if robots.txt and favicon.ico are not found
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }


    # check if a file or directory index file exists, 
    # else pass the request to the index.php as a query parameter.
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # handle execution of PHP files
    # set php5-fpm socket
    # tell NGINX to proxy requests to PHP FPM via the FCGI protocol
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    # block access to .htaccess files
    location ~ /\.ht {
        deny all;
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment