[Nginx] Please help me set root for api and normal site [solved, but seeks simplier approach?]


  • | 1103 points

    I want to set

    all request to mysite.dev/ pointing to /www/mysite/public/ (as usual Laravel)
    then all other api request to mysite.dev/api/* ... to /www/api.mysite/public/ (pointing to Lumen)

    In apache this can be done as

    define ROOT "/var/www/mysite/public/"
    define API "/var/www/api.mysite/public/index.php"
    define SITE "hk.mysite.com"
    <VirtualHost *:80> 
        DocumentRoot "${ROOT}"
        ServerName ${SITE}
    	  ServerAlias *.${SITE}
    	  AliasMatch ^/api(.*) "${API}"
    
        <Directory "${ROOT}">
            AllowOverride All
            Require all granted
        </Directory>
    
    </VirtualHost>
    

    But I cannot figure out how this can be done in Nginx for days.. : /
    Help will be greatly appreciated!!

    The problem is solved.

    server {
        listen 80;
        listen 443 ssl;
        server_name marfair.dev *.marfair.dev;
        root "D:/development/laragon/www/marfair/public/";
        
        index index.html index.htm index.php;
     
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location ^~ /api/ {
            proxy_pass http://127.0.0.1:8080/;
        }
        
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass php_upstream;		
            #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        # Enable SSL
        ssl_certificate "D:/development/laragon/etc/ssl/marfair.dev.crt";
        ssl_certificate_key "D:/development/laragon/etc/ssl/marfair.dev.key";
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
        ssl_prefer_server_ciphers on;
    	
    	
        charset utf-8;
    	
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
        location ~ /\.ht {
            deny all;
        }
    }
    
    server {
        listen 8080;
        listen 4443 ssl;
        server_name marfair.dev *.marfair.dev;
        root "D:/development/laragon/www/api.marfair/public/";
        
        index index.php index.html index.htm ;
     
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
        
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass php_upstream;      
            #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        # Enable SSL
        ssl_certificate "D:/development/laragon/etc/ssl/marfair.dev.crt";
        ssl_certificate_key "D:/development/laragon/etc/ssl/marfair.dev.key";
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
        ssl_prefer_server_ciphers on;
        
        
        charset utf-8;
        
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
        location ~ /\.ht {
            deny all;
        }
    }
    
    

  • | 1103 points

    @leocflam

    The solution is here. Credit to http://unix.stackexchange.com/questions/134087/nginx-server-config-with-multiple-locations-does-not-work

    He gave two approaches. Here is the one I use two server and proxy pass to different port:

    But can anyone provide an approach using ^~ /api/ alias ?

    server {
        listen 80;
        listen 443 ssl;
        server_name mysite.dev *.mysite.dev;
        root "D:/development/laragon/www/mysite/public/";
        
        index index.html index.htm index.php;
     
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location ^~ /api/ {
            proxy_pass http://127.0.0.1:8080/;
        }
        
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass php_upstream;		
            #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        # Enable SSL
        ssl_certificate "D:/development/laragon/etc/ssl/mysite.dev.crt";
        ssl_certificate_key "D:/development/laragon/etc/ssl/mysite.dev.key";
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
        ssl_prefer_server_ciphers on;
    	
    	
        charset utf-8;
    	
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
        location ~ /\.ht {
            deny all;
        }
    }
    
    server {
        listen 8080;
        listen 4443 ssl;
        server_name mysite.dev *.mysite.dev;
        root "D:/development/laragon/www/api.mysite/public/";
        
        index index.php index.html index.htm ;
     
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
        
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass php_upstream;      
            #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    
        # Enable SSL
        ssl_certificate "D:/development/laragon/etc/ssl/mysite.dev.crt";
        ssl_certificate_key "D:/development/laragon/etc/ssl/mysite.dev.key";
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
        ssl_prefer_server_ciphers on;
        
        
        charset utf-8;
        
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
        location ~ /\.ht {
            deny all;
        }
    }
    
    

  • administrators
    | 113923 points

    Thanks @leocflam


  • | 105 points

    Thanks @leocflam
    I also finding solution for this. It would be better if we can use multi location in one block server


Log in to reply
 

Looks like your connection to Laragon was lost, please wait while we try to reconnect.