Search code examples
nginxdnshostingcloudflare

Redirecting a subdomain to a port; not working


server {
        listen         8080 default_server;
        listen         [::]:8080 default_server;
        server_name    cad.domain.tech;
        root           /var/www/cad;
        index          index.php;

          location ~* \.php$ {
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
          }
        } 

I have this in my NGINX site-availables, and when I do IP:8080 I get the webpage I put in there. However, when I do cad.domain.tech I get "server not found".

Here are my page rules and DNS settings: enter image description here enter image description here

Does anybody have any ideas?


Solution

  • This is because when accessing http://cad.domain.tech it requests your server on port 80 by default (Port 443 if the request starts with https://).

    So all you need to do is to redirect all incoming requests on port 80 to port 8080 in your case.

    server {
        listen 80;
        listen [::]:80;
        hostname cad.domain.tech www.cad.domain.tech;
        return 301 http://cad.domain.tech:8080
    }
    

    This should work. return 301 tells the requester that this is meant to be a permanent redirect.