Search code examples
wordpressnginxcloudflarelinode

Nginx subdomain only accessable locally


I'm trying to setup an nginx subdomain on my nginx configuration. But I'm not able to access/browse to my new subdomain remotely. No errors/access logs are registered when trying to access this subdomain remotely. The server is hosted on Linode.

Current setup: default nginx config -> not edited

main site config:

server {
        listen  443 ssl;
        server_name     example.be www.example.be;
        root    /var/www/example;
        index   index.html
        gzip off;

        location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }



    ssl_certificate /etc/letsencrypt/live/example.be/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.be/privkey.pem; # managed by Certbot

}
server {
    if ($host = www.example.be) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = example.be) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



        listen  80;
        server_name     www.example.be;
    return 404; # managed by Certbot

      location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }


}

subdomain config:

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

        root /var/www/sub.example.be;

        index index.html index.htm index.nginx-debian.html index.php;

        server_name sub.example.be;

        location / {
                try_files $uri $uri/ =404;
                auth_basic "Restricted Area";
                auth_basic_user_file /var/www/sub.example.be/.htpasswd;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
                auth_basic "Restricted Area";
                auth_basic_user_file /var/www/sub.example.be/.htpasswd;
        }

        location ~ /\.ht {
               deny all;
        }
}

I'm currently using cloudflare for the main site DNS etc. This was setup a few months ago and works perfectly.

I'm just altering my host file on the server (for local test) and on my home pc (for remote tests).

My curl commands however are not working the same:

server: curl sub.example.be/index.html

<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>

Which is I guess correct behavior.

But when i do that from my home pc: curl -v sub.example.be/index.html

*   Trying [IP]...
* TCP_NODELAY set
* connect to [IP] port 80 failed: Timed out
* Failed to connect to sub.example.be port 80: Timed out
* Closing connection 0
curl: (7) Failed to connect to sub.example.be port 80: Timed out

Oh and yes, I can access my main domain remotely.


Solution

  • My main website was apparently redirecting all traffic to HTTPS.

    What fixed it for me is using DNS verification on certbot to add SSL to my subdomain. Copy the main nginx config and alter it for my subdomain.

    And now I can access both the main and subdomain over HTTPS successfully.