1

I have two different laravel application on my server machine.

They are located at:

D:/APPLICATION/application1

and

D:/APPLICATION/application2

Below is my nginx.conf content:

server {
        listen       80;
        server_name  localhost;        

        location / {
        root "D:/APPLICATION/application1/public";
        try_files $uri $uri/ /index.php?$query_string;
        index index.php index.html index.htm;

        location ~ \.php$ {
            try_files $uri /index.php = 404;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        }

    location ^~ /application2 {
        alias "D:/APPLICATION/application2/public";     
        try_files $uri $uri/ /index.php?$query_string;
        index index.php index.html index.htm;

            location ~ \.php$ {
               try_files $uri /index.php = 404;
               fastcgi_pass  127.0.0.1:9000;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
        }
    }  



     
    }

If I browse http://x.x.x.x/, my first laravel web application comes out perfectly.

But if I browse http://x.x.x.x/application2 I am having No input file specified.

Anything I am missing here?

3
  • better use laragon.org for development Commented Sep 14, 2020 at 8:40
  • for two url i use 1 config file in nginx not in same file as nginx listion for specific port and server name Commented Sep 14, 2020 at 8:45
  • so u want dynamic.? Commented Sep 14, 2020 at 8:52

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

9 Comments

thank you for your answer, but I am running on Windows machine.
thank you very much, I will give you the +100 reward on the other question should we make this work out. thank you very much
I am not sure if I got your question, but I am using it for my laravel application. but i installed nginx manually.
i installed it manually, no 3rd party. i got it from nginx.org/en/download.html
SO said that i can reward the +100 rep from the other question in 3hours.will give it by then.thanks again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.