Cloudflared multiple NGINX websites on different ports

Hello, I am having some trouble setting up two websites using NGINX with cloudflared. I have setup 2 NGINX websites/apps on a Raspberry Pi on different ports, but I don’t know how to go about using the tunnel for the both of them. When I do this configuration, the 1st website works while the other doesn’t. I would appreciate if you could check and advise what I am missing. I can go to each website from using private ip (10.0.0.4 in my case) and see that each port gives the correct website, so I don’t think its NGINX.

My setup:
NGINX:
Two different websites each on a different port:

    Website 1 / App 1: http://localhost:80
    Website 2 / App 2: http://localhost:8080

    Config: (/etc/nginx/sites-available/main.conf)
        server {
            listen 80;
            listen [::]:80;
            root /var/www/<website1>;
            index index.html;
            server_name <website1.com>;

            location / {
                try_files $uri $uri/ =404;
            }
        }    

        server {
            listen 8080;
            listen [::]:8080;
            root /var/www/<website2>;
            index index.html;
            server_name <website2.com>;

            location / {
                try_files $uri $uri/ =404;
            }
        }


Cloudflared:
    One tunnel <id>

    Config: (~/.cloudflared/config.yml)
        tunnel: <id>
        credentials-file: /home/<user>/.cloudflared/<id>.json

        ingress:
          - hostname: <website2.com>
            service: http://localhost:8080

          - hostname: <website1.com>
            service: http://localhost:80

          - service: http_status:404

If you could go ahead and tell me what I am doing wrong, it would help me a ton :grin:

May I ask if you tried with having two config vhost files, which are separated for each website1 and website2 located under the /etc/nginx/sites-available/? :thinking:

Do they respond correctly when you go localhost:port instead of using the IP? :thinking:

You have got no SSL at the origin configured, as per seen by the Nginx main.conf, correct? :thinking:

At Raspberry Pi, if you check netstat is there some process listening on the port 8080, any firewall running like the ufw?

1 Like

Thanks for your reply :smiley:

May I ask if you tried with having two config vhost files, which are separated for each website1 and website2 located under the /etc/nginx/sites-available/? :thinking:

No, since its just 2 websites and their config is really simple, I thought it would be easier to have one file

Do they respond correctly when you go localhost:port instead of using the IP? :thinking:

Yup, both localhost:80 and 8080 work with their corresponding website

You have got no SSL at the origin configured, as per seen by the Nginx main.conf, correct? :thinking:

At Raspberry Pi, if you check netstat is there some process listening on the port 8080, any firewall running like the ufw?

Yes, there is no SSL in nginx as cloudflared adds its own through the tunnel. netstat -apno tells me that nginx is running on both ports, and they are allowed through ufw

1 Like