Last active
October 9, 2024 01:24
-
-
Save tsolar/8d45ed05bcff8eb75404 to your computer and use it in GitHub Desktop.
Laravel in subdirectory nginx example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
client_body_in_file_only clean; | |
client_body_buffer_size 32K; | |
client_max_body_size 300M; | |
sendfile on; | |
send_timeout 300s; | |
# Port that the web server will listen on. | |
#listen 80; | |
# Host that will serve this project. | |
server_name tsolar.com; | |
# Useful logs for debug. | |
access_log /var/log/nginx/tsolar.com-access.log; | |
error_log /var/log/nginx/tsolar.com-error.log; | |
rewrite_log on; | |
# The location of our projects public directory. | |
root /home/tom/public_html/demos/; | |
# Point index to the Laravel front controller. | |
index index.php; | |
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { | |
access_log off; | |
expires max; | |
} | |
location / { | |
# URLs to attempt, including pretty ones. | |
try_files $uri $uri/ /index.php?q=$uri&$args; | |
} | |
# Remove trailing slash to please routing system. | |
if (!-d $request_filename) { | |
rewrite ^/(.+)/$ /$1 permanent; | |
} | |
# version 1 | |
location ^~ /demo1 { | |
alias /home/tom/public_html/demos/demo1/public; | |
try_files $uri $uri/ @demo1; | |
location ~* \.php { | |
fastcgi_pass unix:/var/run/php5-fpm.sock; | |
fastcgi_split_path_info ^(.+\.php)(.*)$; | |
include /etc/nginx/fastcgi_params; | |
} | |
} | |
location @demo1 { | |
rewrite ^/demo1/(.*)$ /demo1/index.php/$1 last; # THIS IS THE IMPORTANT LINE | |
} | |
# end version 1 | |
# version 2 | |
# this is with `ln -s /home/tom/public_html/demos/demo1/public <document root>/demo1` | |
location ~ /demo1 { | |
try_files /demo1/$uri /demo1/$uri/ /demo1/index.php?q=$uri&$args; | |
} | |
# end version 2 | |
# PHP FPM configuration. | |
location ~* \.php$ { | |
fastcgi_pass unix:/var/run/php5-fpm.sock; | |
include /etc/nginx/fastcgi_params; | |
fastcgi_index index.php; | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
fastcgi_param PATH_INFO $fastcgi_path_info; | |
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
} | |
# We don't need .ht files with nginx. | |
location ~ /\.ht { | |
deny all; | |
} | |
# Set header expirations on per-project basis | |
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ { | |
expires 365d; | |
} | |
} |
thanks you so much
@Verron
Works For me ,
Laravel 10
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html/v2api;
index index.php index.html;
location /v2api {
alias /var/www/html/v2api/public/;
try_files $uri $uri/ @v2api;
# location ~* ^/v2api/(.*\.php)$ {
location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass v2api:9001;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
# fastcgi_param SCRIPT_FILENAME $request_filename;
# include fastcgi_params;
}
}
location @v2api {
rewrite /v2api/(.*)$ /v2api/index.php last;
}
location / {
try_files $uri $uri/ /index.php?s=$uri&$args;
}
location ~ \.php {
add_header 'Access-Control-Allow-Origin' $allow_origin;
add_header 'e-flag' $allow_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header Access-Control-Allow-Methods 'PUT,DELETE,GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,XO,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
fastcgi_pass v1api:9000;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
qapi/api/uptime?a=b
"REQUEST_URI": "/qapi/api/uptime?a=b",
"QUERY_STRING": "a=b",
The following config worked for me. Here's a video of it in action: Run Laravel via subdirectories on a Nginx server
server {
listen 80;
listen [::]:80;
server_name yourdomain.com;
root /var/www/main/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location /admin {
alias /var/www/admin/public;
try_files $uri $uri/ @admin;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/admin/public/index.php;
}
}
location @admin {
# Take any requests that start with /admin/ and rewrite them to /admin/index.php (Laravel’s front controller)
rewrite /admin/(.*)$ /admin/index.php last;
}
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
location = /favicon.ico {
access_log off; log_not_found off;
}
location = /robots.txt {
access_log off; log_not_found off;
}
error_page 404 /index.php;
charset utf-8;
}
I have done that by using below configuration and its working. The above mentioned gist was displaying 404 "Not Found".
# sub_directory location ^~ /sub_directory { alias /var/www/choppies/sub_directory/public; try_files $uri $uri/ @sub_directory; location ~ \.php { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_param SCRIPT_FILENAME /var/www/choppies/sub_directory/public/index.php; } } location @sub_directory { rewrite /sub_directory/(.*)$ /sub_directory/index.php?/$1 last; } # end sub_directory
worked for me, thanks @SuryaElite
As of Aug'24, this worked for me:
server {
listen 443;
server_name www.example.com example.com;
charset utf-8;
root /var/www/www.example.com;
access_log /var/log/nginx/ssl-access.log;
error_log /var/log/nginx/ssl-error.log;
rewrite_log on;
# development settings
client_max_body_size 50M;
fastcgi_buffers 8 1600k;
fastcgi_buffer_size 3200k;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include snippets/ssl-params.conf;
location ~ /.well-known {
allow all;
}
location ^~ /laravel {
alias /application/laravel/public;
access_log /var/log/nginx/laravel-access.log;
error_log /var/log/nginx/laravel-error.log error;
try_files $uri $uri/ @laravel;
location ~ \.php$ {
fastcgi_split_path_info ^\/laravel\/((?U).+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME /app/public/$fastcgi_script_name;
fastcgi_param SCRIPT_NAME /laravel/$fastcgi_script_name;
}
}
location @laravel {
rewrite /laravel/(.*)$ /laravel/index.php last; # Remove ?/$1 since fastcgi_params adds query string
}
... other stuff for my website ...
}
I had to adapt the examples posted by other users because the FPM daemon is running within a docker container, but nginx is not
And nginx does need access to the files to serve them, hence:
- The need to use an alias where the app files are present.
- The need to set the container path (/app) on the
SCRIPT_FILENAME
fastcgi_param
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might need an extra line
in the
location .php {
block to mitigate https://httpoxy.org vulnerabilities.