different domains, pointing to Apache or Nginx, on same port

I have my website running on one machine, across two web servers; Apache for handling PHP content and Nginx for serving static files. They both point to the same document root on my linode, but use different domains and ports to clarify which server you are using.

For example, example.com is for Apache, whilst static.com:8080 will go to Nginx.

This setup works great, except that some users have port 8080 blocked (such as when they are at the office), and so they cannot download the static content which makes the site unusable. I want to remove the port 8080 from my static domain, and have both servers appear to be listening on port 80.

How would I go about achieving this?

One idea I had was to (kind of) redirect static.com internally to port 8080, somewhere between the request coming in and it hitting Apache or Nginx, but after Googling around I could not find a way to achieve this.

Another solution is to take down Apache or Nginx, and have just one server, but I want to continue to use both for the same site, side by side.

I am open to any other ideas.

2 Replies

Easiest way would be to have nginx listening on port 80 and Apache listening on some localhost port. If a request is deemed to need Apache, use proxy_pass to send the request there.

Since you have it split up by host, this could be as trivial as:

server {
  server_name example.com;
  location / {
    proxy_pass http://127.0.0.1:8080/;
    # etc...
  }
}

server {
  server_name static.example.com;
  root /srv/www/example.com/public_html/;
  location ~ \.php$ {
    rewrite ^(.*)$ http://example.com/$1 permanent;
  }
  location / {
    # blah blah...
  }

On the second blurb there, note that I stuck a redirect in there for anything ending in .php… I did it the "not quite right" way (the location block is a little redundant) to suggest that you can also combine the two and do it all on one domain! See this library article for more on that.

Thanks, I've got my nginx proxy setup now. However that article doesn't really go into enough depth; I had several redirect issues with wordpress. I found this far more useful.

For anyone else finding this post, after a similar solution, I have setup my nginx to forward everything on to apache, in nginx.conf. This way if I add more sites to apache, they should 'just work'.

This is what my my default server settings in nginx.conf looks like:

server {
    # the IP of my Linode box
    # if you use this, change it to yours
    listen 192.168.1.67:80;

    location / {
        proxy_pass http://127.0.0.1; # redirect to apache

        proxy_redirect off;
        proxy_buffering off;

        # needed to prevent some apache redirects to localhost
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct