skip to Main Content

Good Morning guys,

i am trying to create an working Nginx Config.

I have two web applications:

  • /app/web
  • /app/api

My URL should look like this:

  • 10.X.X.XX => /app/web

  • 10.X.X.XX/api => /app/api

My current config:

server {
    listen 80 default_server;
 
    index index.php index.html index.htm;

    root /app/web;

    location /api {
      root /app/api;
    }

    location ~ [^/].php(/|$) {
        autoindex on;
        autoindex_exact_size on;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    error_log  stderr warn;
    access_log  /dev/stdout main;

    client_max_body_size 10M;
}

Any suggestions?

3

Answers


    1. Just copy and paste the same config into a new file
    2. Name it the same as the subdomain you want it to run on
    3. Give the path to the folder
    4. Add the new subdomain to the hosts file
    5. Restart nginx
    Login or Signup to reply.
  1. Create 2 more servers in nginx. The first for /api (listen en 8080 for example), the other for /web (on 8081). Your main serveur (on 80/443) is then a proxy on the others :

    upstream backend_api{
        server 127.0.0.1:8080;
    }
    
    upstream backend_web{
        server 127.0.0.1:8081;
    }
    
    server {
        listen    80;
        server_name    www.example.com example.com;
    
        location /api{
            include proxy_params;
            proxy_pass http://backend_api;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://backend_web;
        }
    }
    
    server {
        listen 8080 default_server;
     
        index index.php index.html index.htm;
    
        root /app/api;
        
        location ~ [^/].php(/|$) {
            autoindex on;
            autoindex_exact_size on;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    
        error_log  stderr warn;
        access_log  /dev/stdout main;
    
        client_max_body_size 10M; }
    
    server {
        listen 8081 default_server;
     
        index index.php index.html index.htm;
    
        root /app/web;
       
        location ~ [^/].php(/|$) {
            autoindex on;
            autoindex_exact_size on;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    
        error_log  stderr warn;
        access_log  /dev/stdout main;
    
        client_max_body_size 10M;
    }
    
    Login or Signup to reply.
  2. You can host multiple site if you follow below configuration. This is a working code. You can modify it according to your need

    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;
        }
    }`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search