Skip to content

Instantly share code, notes, and snippets.

@ashleydw
Last active June 19, 2024 17:08
Show Gist options
  • Save ashleydw/afd389b1e763d3c1cf1f to your computer and use it in GitHub Desktop.
Save ashleydw/afd389b1e763d3c1cf1f to your computer and use it in GitHub Desktop.
Laravel nginx conf file
server {
listen 80 default_server;
server_name example.com www.example.com;
access_log /srv/www/example.com/logs/access.log;
error_log /srv/www/example.com/logs/error.log;
root /srv/www/example.com/public;
index index.php index.html;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
log_not_found off;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock; # may also be: 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
@dhcmega
Copy link

dhcmega commented Aug 30, 2018

Hi, I changed the php version to a more updated one:

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock

@kmkmjhyiiiu
Copy link

@dhcmega
it's depends on us like if our laravel requires 5.6 then we may not change it. But like if install fresh and latest one then we can use php 7 one as you said.

@djcyphers
Copy link

Arch Linux (nginx.conf):
fastcgi_pass unix:/var/run/php5-fpm.sock;

Change to:
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;

@erlangparasu
Copy link

thanks 👍

@robkerr1992
Copy link

Thank you!!! Been scratching my head all weekend trying to figure out the right nginx config. You're a life saver.

@ivan-avalos
Copy link

ivan-avalos commented Jul 27, 2019

I finished with this file for Docker:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

    # serve static files directly
	location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
		access_log off;
		expires max;
		log_not_found off;
	}

	# removes trailing slashes (prevents SEO duplicate content issues)
	if (!-d $request_filename)
	{
		rewrite ^/(.+)/$ /$1 permanent;
	}

	# enforce NO www
	if ($host ~* ^www\.(.*))
	{
		set $host_without_www $1;
		rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
	}

	# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
	if (!-e $request_filename)
	{
		rewrite ^/(.*)$ /index.php?/$1 last;
		break;
	}

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

	location ~* \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
		deny all;
	}
}

@ColinMichaels
Copy link

Thanks this was exactly what I was looking for!

@bernhardh
Copy link

Not sure, but do we really need /$1 in the line

rewrite ^/(.*)$ /index.php?/$1 last;

Because with this, it always adds the path to the query, which we dont need.

@GempitaRizki
Copy link

Does this solve the problem when it takes a long time to access our website/project for the first time? But after opening it becomes fast?

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