Skip to content

Instantly share code, notes, and snippets.

@erssebaggala
Forked from mreschke/nginx.conf
Created April 7, 2020 02:20
Show Gist options
  • Save erssebaggala/544f3cb35909d840ebed77bbde7ba4e4 to your computer and use it in GitHub Desktop.
Save erssebaggala/544f3cb35909d840ebed77bbde7ba4e4 to your computer and use it in GitHub Desktop.
Nginx config for multiple laravel sites based on /api/v1 url paths
# Handles main / site plus plus additional /api/v1 sites
server {
# Listing port and host address
listen 80;
server_name example.com;
# Default index pages
index index.php;
# Default character set
charset utf-8;
# Turn off /var/log/nginx/access.log writes
access_log off;
log_not_found off;
# Send file is an optimization, but does not work
# across unix sockets which I use for php fpm so is best
# used for local static content only
sendfile off;
# Root for / project
root /var/www/example/public;
# Handle main root / project
location / {
try_files $uri $uri/ /index.php?$args;
}
# Handle api/v1 sub project
location /api/v1 {
# Debug output
#return 200 $args; add_header Content-Type text/plain;
# Root for this sub project
root /var/www/api-v1/public;
# Rewrite $uri=/api/v1/xyz back to just $uri=/xyz
rewrite ^/api/v1/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
# Handle all locations *.php files (which will always be just /index.php)
# via factcgi PHP-FPM unix socket
location ~ \.php$ {
# At this piont, $uri is /index.php, $args=any GET ?key=value
# and $request_uri = /api/v1/xyz. But we DONT want to pass
# /api/v1/xyz to PHP-FPM, we want just /xyz to pass to
# fastcgi REQUESTE_URI below. This allows laravel to see
# /api/v1/xyz as just /xyz in its router. So laravel route('/xyz') responds
# to /api/v1/xyz as you would expect.
set $newurl $request_uri;
if ($newurl ~ ^/api/v1(.*)$) {
set $newurl $1;
root /var/www/api-v1/public;
}
#return 200 $args; add_header Content-Type text/plain;
#return 200 $uri; add_header Content-Type text/plain;
#return 200 $document_root; add_header Content-Type text/plain;
#return 200 $request_uri; add_header Content-Type text/plain;
#return 200 $newurl; add_header Content-Type text/plain;
# No need for rewrite, as we will use $newurl above.
#rewrite ^/api/v1/index.php(.*)$ /$1 break;
#rewrite ^/index.php(.*)$ /$1 break;
#return 200 $uri; add_header Content-Type text/plain;
# Pass all PHP files to fastcgi php fpm unix socket
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass unix:/var/run/php5-fpm.sock; #debian php5
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; #debian php7
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment