Nginx – running php app and node on the same domain with different ports

Question:

I'm setting up blocks in nginx, but I'm having trouble running two different apps, see the following situation:

  • I have 2 apps (PHP and Node.js)
  • just one domain (example.com.br) and I need to point port 80 to the PHP application and port 8080 to the node.js app.
  • I've already noted the PHP app and it's ok, but I can't access the app node on the 8080

See my block file for php

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name exemplo.br;

    # root directive should be global
    root   /var/www/app-php/public;
    index  index.php;

    access_log /var/www/app-php/log/access.log;
    error_log  /var/www/app-php/log/error.log;

    client_max_body_size 1024M;

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


    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
   }

   location  ~ /\.ht{
       deny all;
   }

}

E o block do node.js

server {
    listen 8080;

    location / {
            proxy_pass http://localhost:3000;
            # proxy_pass http://127.0.0.1:3000;
            # proxy_pass http://IP-PRIVADO-SERVER:3000;
            # proxy_pass http://exemplo.com.br:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host:8080;
            # proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }

}

Can anyone tell me what could be wrong, please?

Note: on the server I am configuring nginx does not have iptables installed and I have already tested the node.js application on port 80 and it worked normally.

Answer:

I managed to find the solution, there were two problems that were interrupting the execution of this targeting.

1. 8080 is an alias for port 80, so it would not be possible to do this for the same domain. Solution I switched to port 81

2. the server I was configuring has an external firewall, so it blocked other dynamic ports that only worked internally using curl -s dominio.com.br:3000

Scroll to Top