1

I currently have a working reverse proxy to a local domain: https://domain.test with an entry in /etc/hosts

127.0.0.1 domain.test

https://domain.test goes to an spa website while https://domain.test/api goes to a laravel to handle all api calls.

I would like to have a subdomain e.g. https://sub.domain.test and have another api server to handle https://sub.domain.test/api

This is my current /etc/nginx/sites-enabled/domain.conf First server block redirects to https. https://domain.test is handled by spa while https://domain.test/api is reverse proxied by port 8282.

access_log /var/log/nginx/domain-access.log;
error_log /var/log/nginx/domain-error.log;

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

    return 301 https://domain.test$request_uri;
}

server {
   listen 443 ssl http2 default_server;
   listen [::]:443 ssl http2 ipv6only=on default_server;

   ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
   ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
   
   server_name domain.test;

   location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_ssl_server_name on;
        proxy_set_header Upgrade $http_update;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
   }

    location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8282;
    }
}

server {
    listen 8282;

    server_name localhost;
    root /home/gmhafiz/domain/public;
    index index.php;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    error_log /var/log/nginx/nginx-proxy-laravel.error.log;
    access_log /var/log/nginx/nginx-proxy-laravel.access.log;
}

I tried adding two new server blocks to handle subdomain for https://sub.domain.test and added an entry to /etc/hosts

server {
    server_name sub.domain.test;

    location /api {
        proxy_pass http://127.0.0.1:8283;
    }
}

server {
    listen 8383;

    server_name localhost;
    root /home/gmhafiz/projects/subdomain/public;
    index index.php;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock;
            fastcgi_index index.php;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_read_timeout 600;
            include fastcgi_params;
    }

    error_log /var/log/nginx/nginx-proxy-laravel-subdomain.error.log;
    access_log /var/log/nginx/nginx-proxy-laravel-subdomain.access.log;
}

but it doesn't work because I think the first server block redirects with 301.

I cannot add duplicate server block that listens to both 80 and 443 either. I still need https://domain.test to work.

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

    return 301 https://queue.dribl.test$request_uri;
}

I've done both sudo nginx -t and sudo systemctl restart nginx.service

I made a test controller for the subdomain api to handle https://sub.domain/api/test curl -k --location --request GET 'https://sub.domain.test/api/test' but it returns

{"error": "Endpoint not found."}

Both /var/log/nginx/domain-access.log has a log entry:

127.0.0.1 - - [08/Jan/2021:13:51:36 +1100] "GET /api/test HTTP/2.0" 400 31 "-" "curl/7.68.0"

/var/log/nginx/nginx-proxy-laravel.access.log

127.0.0.1 - - [08/Jan/2021:13:51:36 +1100] "GET /api/test HTTP/1.0" 400 31 "-" "curl/7.68.0"

/var/log/nginx/nginx-proxy-laravel-subdomain.error.log and /var/log/nginx/nginx-proxy-laravel-subdomain.access.log are empty

So clearly the first block must be fixed to allow subdomain access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.