Get the most out of your Centmin Mod LEMP stack
Become a Member

Nginx Config Nginx as reverse proxy for Nodejs

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by Kuro, May 23, 2019.

Tags:
  1. Kuro

    Kuro Member

    78
    8
    8
    Feb 8, 2018
    Ratings:
    +15
    Local Time:
    4:22 PM
    1.15.10
    10.3
    • CentOS Version: CentOS 7
    • Centmin Mod Version Installed: 123.09beta01
    • When was last time updated Centmin Mod code base ? : just now
    Hi,
    I'm attempting to config nginx as reverse proxy for my nodejs project but failed.
    Nginx port 80, Nodejs port 3014
    This is my /usr/local/nginx/conf/conf.d/i{my_server_here}.conf
    Code:
    server {
      listen   80;
      server_name {my_server_here} www.{my_server_here};
    
      add_header X-Xss-Protection "1; mode=block" always;
      add_header X-Content-Type-Options "nosniff" always;
    
      include /usr/local/nginx/conf/autoprotect/{my_server_here}/autoprotect-{my_server_here}.conf;
      include /usr/local/nginx/conf/503include-main.conf;
    
      location / {
        proxy_pass http://{my_ip_here}:3014;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
      }
    
    
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
    
      include /usr/local/nginx/conf/drop.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
    
    Open port in csf too
    Code:
    # Allow incoming TCP ports
    TCP_IN = "20,21,22,25,53,80,110,143,161,443,465,587,993,995,1110,1186,1194,81,9418,30001:50011,3014,3000"
    
    # Allow outgoing TCP ports
    TCP_OUT = "8080,2525,465,1110,1194,9418,20,21,22,25,53,80,110,113,443,587,993,995,3014,3000"
    But it still not working, did I miss something? Please help! Thanks!
     
  2. eva2000

    eva2000 Administrator Staff Member

    42,344
    9,559
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,730
    Local Time:
    7:22 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    you don't need to whitelist port in CSF Firewall as nginx is connecting to nodejs port internally

    but what do you mean doesn't work ? errors ? symptoms ? ensure that nodejs app is running on the listening 3014 port first
     
  3. Kuro

    Kuro Member

    78
    8
    8
    Feb 8, 2018
    Ratings:
    +15
    Local Time:
    4:22 PM
    1.15.10
    10.3
    502 bad gateway, nodejs already listening on port 3014. http://my_ip display Centmin test page
     
  4. eva2000

    eva2000 Administrator Staff Member

    42,344
    9,559
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,730
    Local Time:
    7:22 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    502 bad gateway means nginx sees nodejs as down/not running or unable to connect to it so problem is your nodejs app
     
  5. Kuro

    Kuro Member

    78
    8
    8
    Feb 8, 2018
    Ratings:
    +15
    Local Time:
    4:22 PM
    1.15.10
    10.3
    I'm installed nodejs first at "/home/myapp" and it running normaly then I used your mod to install nginx for reverse proxy. Do I need config anything to make it work? Sorry, I'm new to nginx so I don't really know what to do
     
  6. Kuro

    Kuro Member

    78
    8
    8
    Feb 8, 2018
    Ratings:
    +15
    Local Time:
    4:22 PM
    1.15.10
    10.3
    Oh, sorry, my bad, this
    Code:
    proxy_pass http://{my_ip_here}:3014;
    should be
    Code:
    proxy_pass http://127.0.0.1:3014;
    I thought it was ipv4, thank for help!