Last active
February 3, 2025 21:23
-
-
Save arif98741/c84fa229a343a1b984f736e2ecba06f6 to your computer and use it in GitHub Desktop.
Host Multiple Laravel Project on Same Port 80 Nginx. https://www.programmingmindset.com/post/host-multiple-laravel-project-on-same-port-in-nginx-server
This file contains hidden or 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
# Nginx.conf | |
# Project 1(Path: /var/www/msdsl/shipment, Url: http://192.168.0.132) | |
# Project 2(Path: /var/www/msdsl/restora, Url: http://192.168.0.132/restora) | |
# Project 3(Path: /var/www/msdsl/tposreport, Url: http://192.168.0.132/tposreport) | |
server { | |
# Listing port and host address | |
# If 443, make sure to include ssl configuration for the same. | |
listen 80; | |
listen [::]:80; | |
server_name 192.168.0.132; | |
# Default index pages | |
index index.php; | |
# Root for / shipment | |
root /var/www/msdsl/shipment/public; | |
# Handle main root / shipment | |
location / { | |
#deny all; | |
try_files $uri $uri/ /index.php?$args; | |
} | |
# Handle restora project, just replicate this section for further projects app3, app4 | |
# by just replacing restora with appropriate tag(project1,/project2/project 3) | |
location /restora { | |
# Root for this project | |
root /var/www/msdsl/restora/public; | |
# Rewrite $uri=/restora/xyz back to just $uri=/xyz | |
rewrite ^/restora/(.*)$ /$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; | |
} | |
location /tposreport { | |
# Root for this project | |
root /var/www/msdsl/tposreport/public; | |
# Rewrite $uri=/tposreport/xyz back to just $uri=/xyz | |
rewrite ^/tposreport/(.*)$ /$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 point, $uri is /index.php, $args=any GET ?key=value and $request_uri = /restora/xyz. | |
# But we don't want to pass /restora/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below. | |
# This allows laravel to see /restora/xyz as just /xyz in its router. | |
# So laravel route('/xyz') responds to /restora/xyz as you would expect. | |
set $newurl $request_uri; | |
if ($newurl ~ ^/tposreport(.*)$) { | |
set $newurl $1; | |
root /var/www/msdsl/tposreport/public; | |
} | |
if ($newurl ~ ^/restora(.*)$) { | |
set $newurl $1; | |
root /var/www/msdsl/restora/public; | |
} | |
# Pass all PHP files to fastcgi php fpm unix socket | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
# Use php fpm sock which is installed on your machine like php7.2, php5.6 | |
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; | |
fastcgi_index index.php; | |
include fastcgi_params; | |
# Here we are telling php fpm to use updated route that we've created to properly | |
# response to laravel routes. | |
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; | |
} | |
# Deny .ht* access | |
location ~ /\.ht { | |
deny all; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment