Ferna Stereo
Ferna Stereo

Reputation: 337

How to serve two laravel projects in nginx server with IP address?

I setup a laravel project on Ubuntu server with nginx, now I need to deploy a second project in the same server, if I don't have a domain name but IP address, how must I define the server_name for my second project? I tried with XX.XX.XX.XX/mysecondproject but I had an error when I reload nginx. I really appreciate if you can help me with this question. Here is my nginx file.

server {
listen 80;
listen [::]:80;

root /var/www/mysecondproject/public;

index index.php index.html index.htm index.nginx-debian.html;

server_name XX.XX.XX.XX/mysecondproject;

location / {
        try_files $uri $uri/ /index.php?$query_string;
}

error_page 404 /index.php;

location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
}

}

Upvotes: 2

Views: 5510

Answers (2)

Ferna Stereo
Ferna Stereo

Reputation: 337

this approach worked for me. This way I have a first_project running at IP address (XX.XX.XX.XX) and a second application running at XX.XX.XX.XX/my_second_project. I used only the /etc/nginx/sites-available/default file for setup my server. I hope it helps somebody in the future.

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  root /var/www/my_first_project/public;

  index index.php index.html index.htm index.nginx-debian.html;

  server_name XX.XX.XX.XX //my IP Address;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location /my_second_project {
    alias /var/www/my_second_project/public;
    try_files $uri $uri/ @my_second_project;

    location ~ \.php$ {
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $request_filename;
      include fastcgi_params;
    }
  }

  location @my_second_project {
    rewrite /my_second_project/(.*)$ /my_second_project/index.php?/$1 last;
  }

  error_page 404 /index.php;

  location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    include fastcgi_params;
  } 
}

Upvotes: 8

Timo Stark
Timo Stark

Reputation: 3071

If you have a single ip addr for the instance like 10.0.1.20 and you want to serve both projects over the same ip address you can try the following configurations.

Seperated by Port

Your instance is lisiting on IP 10.0.1.20. Port 80 is the your App1 and Port 8080 is your App2.

#App1
server {
   server_name 10.0.1.20;
   listen 80;

   .....

}
#App2
server {
   server_name 10.0.1.20;
   listen 8080;

   .....

}

If you want to seperate your apps by location (/app1, /app2) and not by port.

Seperate by Location

#AppServer 1
server {
   listen 8081;
   ......
}

#AppServer 2
server {
   listen 8080;
   ......
}


#ProxyServer
server {
   listen 80;
   server_name 10.0.1.20;

   location /app1 {
       proxy_pass http://localhost:8080/;
       proxy_set_header Host $host;
       ......

   }
   location /app2 {
       proxy_pass http://localhost:8081/;
       proxy_set_header Host $host;
       ....
   }
}

Upvotes: 1

Related Questions