Cannot start and configure Nginx reverse proxy due to running Apache

March 26, 2019 590 views
Nginx Node.js Apache WordPress

I installed a wordpress droplet. It’s using an Apache server on Ubuntu 18.04.

My app only uses wordpress for the dashboard and API. I have a nextjs node/react app running with server-side rendering to display the actual website.

So, I’m following the tutorial on configuring the nodejs app: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-18-04

Therefore, I was trying to set up the reverse proxy with Nginx. However, Nginx servise is inactive (dead), and I cannot start it. Looks like this is because I have Apache running at the same time.

What should I do in this case?

2 Answers

What I can think up is that Apache in binding to the port 80 (as default) and when nginx tries to do the same it fails, as two apps can’t bind to the same port.

I think the easier solution, would be to change Apache’s port, and have nginx with it reverse proxy redirect the WordPress traffic to Apache new port.

  • Thanks, I’m quite new to this server-side stuff. I’ll have to find how that should actually be implemented.

    Originally I wanted nginx for reverse proxy redirecting to port 8080 where my node app will live. So can I still do that after doing what you suggest (redirect wordpress traffic to apache new port)?

    • Yes, you can have Apache listening in the 8090 port, for example. And have two server configs in the nginx server.

      If example1.com route to 8080
      If example2.com route to 8090

      The Apache port config should be here /etc/apache2/ports.conf, and you would need to change the line Listen 80 to the port of your choosing.

      Then in the nginx setup, you would make the same configuration you’re doing for your NodeJS app to port 8080, but for port 8090.

      • Thanks for your help. I was able to set up both proxies. Now if I curl localhost:8080 or curl localhost:8090 I get the correct response.

        But do you happen to know how I can access these in browser? If I just hit my public ip home page, I’m still getting the Nginx default page.

        • You need to add the information to your nginx configuration, for it to route the traffic.

          server {
            listen 80;
            server_name  example.org  www.example.org;
          
            # Reverse Proxy
            location / {
              proxy_pass http://localhost:$PORT;
              proxy_set_header Host               $host;
              proxy_set_header X-Real-IP          $remote_addr;
              proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
            }
          }
          

          That’s an example config. You’d need to have two Server blocks, one per port. The proxy_set_header might not be needed, I’m just copying an old config I used.

          If both apps use the same domain (or IP), you can just have in the same server block, two location blocks. For example.

            # Reverse Proxy
            location / {
              proxy_pass http://localhost:$PORT;
              proxy_set_header Host               $host;
              proxy_set_header X-Real-IP          $remote_addr;
              proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
            }
          
            location /nodeApp {
              proxy_pass http://localhost:$PORT;
              proxy_set_header Host               $host;
              proxy_set_header X-Real-IP          $remote_addr;
              proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
            }
          }
          

          The latter, will allow yo to have example.org go to WordPress for example, and example.org/nodeApp to the Node.js app.

Have another answer? Share your knowledge.