Nginx: Serve Multiple Laravel Apps with Same Url But Two Different Sub Locations in Linux

nginx config for two Laravel projects in different location

I solved using another port
some thing like that

server {
listen 85 ;
listen [::]:85 ;
server_name ip:85 ;
root /var/www/html/projectfolder1;
}
server {
listen 90 ;
listen [::]:90 ;
server_name ip:90 ;
root /var/www/projectfolder2;
}

hope that help some one

Multiple Laravel Applications Using Nginx - Windows

For windows use fastcgi_pass as 127.0.0.1:9000 instead of unix socket.

Please make sure your php cgi is running. If not, you can start it by

1. Open command prompt
2. Go to path of php-cgi file. (e.g. C:\php-7.3.11, here you'll find fast-cgi.exe).
2. php-cgi.exe -b 127.0.0.1:9000

Nginx configuration with rewrite module.

# Nginx.conf
# App 1(Path: D:/APPLICATION/application1, Url: http://localhost)
# App 2(Path: D:/APPLICATION/application2, Url: http://localhost/application2)
server {
# Listing port and host address
# If 443, make sure to include ssl configuration for the same.
listen 80;
listen [::]:80;

server_name localhost;

# Default index pages
index index.php;

# Root for / project
root "D:/APPLICATION/application1/public";

# Handle main root / project
location / {
#deny all;
try_files $uri $uri/ /index.php?$args;
}

# Handle application2 project
location /application2 {
# Root for this project
root "D:/APPLICATION/application2/public";

# Rewrite $uri=/application2/xyz back to just $uri=/xyz
rewrite ^/application2/(.*)$ /$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$ {
# We don't want to pass /application2/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below.
# So laravel route('/xyz') responds to /application2/xyz as you would expect.
set $newurl $request_uri;
if ($newurl ~ ^/application2(.*)$) {
set $newurl $1;
root "D:/APPLICATION/application2/public";
}

# Pass all PHP files to fastcgi php fpm unix socket
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Use php fastcgi rather than php fpm sock
fastcgi_pass 127.0.0.1:9000;
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;
}

# Deny .ht* access
location ~ /\.ht {
deny all;
}
}

Note: When we're using session based laravel setup, all the route generator functions(url(), route()) use hostname localhost as root url not localhost/application2. To resolve this issue please do following changes in laravel app.

  1. Define APP_URL in .env file as APP_URL="localhost/application2"
  2. Go to RouteServiceProvider which is located at app/Providers/RouteServiceProvider and force laravel to use APP_URL as root url for your app.
public function boot()
{
parent::boot();
// Add following lines to force laravel to use APP_URL as root url for the app.
$strBaseURL = $this->app['url'];
$strBaseURL->forceRootUrl(config('app.url'));
}

Update: Make sure to run php artisan config:clear or php artisan config:cache command to load the updated value of APP_URL.

For Linux System : Nginx: Serve multiple Laravel apps with same url but two different sub locations in Linux

nGinx, multiple locations serving different SPAs

I do not understand why the URI /spa1/ would return the file /var/www/index.html, with your current configuration. However...

The value of the root statement should be /var/www for all three cases. The path to the file is calculated by concatenating the value of the root statement with the URI.

So your directory structure and URI plan should work with the even simpler configuration of:

server {
root /var/www;
index index.html;

error_page 404 /404.html;
}

See this document for details.



Related Topics



Leave a reply



Submit