2
votes

Now, I have two projects(sites) based on Laravel + PHP + MySQL + Nginx, vistors can access them by typing:

  http://www.mysite.com:80
  http://www.mysite.com:8001

Can I change the accessing method to virtual folder not by port?

  http://www.mysite.com/project1
  http://www.mysite.com/project2

The nginx conf files are (at /etc/nginx/conf.d/):

  • project1.conf

    server {
        listen *:80;
        server_name mysite.com www.mysite.com;
        server_tokens off;
        root /var/www/html/project1/public;
        client_max_body_size 100m;
        access_log  /var/log/nginx/project1_access.log;
        error_log   /var/log/nginx/project1_error.log;
        location / {
                index  index.php index.html;
    
    
            if (!-f $request_filename){
                    rewrite (.*) /index.php;
            }
    }
    
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    
    }
  • project2.conf

    server {
        listen *:80;
        server_name www.mysite.com;
        server_tokens off;
        root /var/www/html/project2/public;
        client_max_body_size 100m;
        access_log  /var/log/nginx/project2_access.log;
        error_log   /var/log/nginx/project2_error.log;
    
    
    location / {
        index  index.php index.html;
    
        if (!-f $request_filename){
            rewrite (.*) /index.php;
        }
    }
    
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    
    }
2

2 Answers

0
votes

The only decent way to do it is by folder and virtual host rather than port.

0
votes

Sure - an example config will look like this:

1 Define your app servers

server {
 listen 8080;
 root /var/www/html/project1/public;
  ......
}
server {
 listen 8081;
 root /var/www/html/project2/public;
  ......
}

2 Define your proxy server

server {
 listen 80;
 server_name mysite.com www.mysite.com;
 .....
 location /project1 {
    proxy_pass http://localhost:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    .....
  }


  location /project2 {
    proxy_pass http://localhost:8081;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    ....
  }
}