0

I'm running on the same server Apache (port 81) and NGINX (port 80). For www.example.com I have the below in the NGINX config file.

The problem is when Apache is started on this server ALL requests for www.example.com are going to the Apache server on port 81. This is not configured in the upstream and www.example.com is also not configured in the local Apache instance so errors all over.

I do not want this to go local but only external as defined in the upstream.

When i stop apache the upstream is used and the request from nginx are going to the servers defined in the upstream, when i start Apache again all requests are routed local to Apache

So how can i avoid the requests going to Apache when it's loaded and let nginx always use the defined upstream?

upstream mainserver {
    server w1.externalserver.nl;
    server w2.externalserver.nl;
}

server {
listen 80;

server_name www.example.com;

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://mainserver;

}
}
2
  • Check your firewall. Feb 28, 2018 at 16:16
  • For what as all works fine stopping Apache but if Apache is loaded it routes all requests local, how would the firewall be involved?
    – Robvk
    Mar 1, 2018 at 6:44

1 Answer 1

0

I suggest to define the port number in nginx configs:

upstream mainserver {
    server w1.externalserver.nl:81;
    server w2.externalserver.nl:81;
}

And check that Apache listens on Your correct port. For Ubuntu installations: apache2.conf or ports.conf (check this file too, it may content rewrite settings):

Listen 127.0.0.1:81

Update 1

+-------------+
| server 1    | <------- http request :80
|             |
| nginx ----+ | -------> redirect to server 2 (You want)
|           | |
| Apache <--+ | internal redirect to local Apache (problem place)
+-------------+

1). On server 1 run both Apache and nginx. curl -I http://example.com:80 Check field "Server" in response. If there is Apache is present - check carefully all it configs: apache2.conf, ports.conf, virtual hosts configs - somewhere mentioned port 80 and Apache "rewrite" nginx "listen".

2). Try proxy_pass http://mainserver; to proxy_pass mainserver; - without protocol.

12
  • This is correct for the external server port that runs on 80 not port 81, only the local Apache server is running on port 81 and listening correctly. This as mentioned is the issue, all requests are handled local on Apache which they should not
    – Robvk
    Mar 1, 2018 at 6:43
  • @Robvk Ok. I re-read Your question :) Apache is running and listening 81, and request after nginx must go not to this Apache, but to the external server. 1). First of all look at output sudo apachectl -S 2). Also, check ports.conf - does there exists Listen 127.0.0.1:81. Mar 1, 2018 at 12:04
  • It has *:81 however thats not the issue, nginx should not go there in the first place as the upstream clearly has only the 2 external servers defined so why is nginx still going to the local apache when its loaded on the server. I have not setup the local apache to host the www.example.com and i don't want to do this either.
    – Robvk
    Mar 1, 2018 at 12:33
  • @Robvk 1). sudo apachectl -S - what is there? 2). When You make request - port number is present in it (for example http:example.com:80 in browser)? 3). If You just execute from workstation curl -I http://example.com:80 - what is in "Server" field in response? I suppose problem not in nginx config - it looks good. Mar 1, 2018 at 13:52
  • 1) *:81 is a NameVirtualHost 2) this gets me the site when apache is not running on the local server and an error when apache is running on the local server. This error is correct as apache on the local server is not configured for www.example.com and shouldn't be 3) Server: nginx/1.10.3 (Ubuntu)
    – Robvk
    Mar 1, 2018 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .