ansible-nginx icon indicating copy to clipboard operation
ansible-nginx copied to clipboard

Upstream-servers don't work, ends up in webroot instead

Open ScuttleSE opened this issue 8 years ago • 12 comments
trafficstars

Got a site defined like this:

  example.se:
    domains: ['example.se', 'www.example.se']
    default_server: False
    upstreams:
      - name: 'example'
        servers: ['hostname_of_internal_server:80']

When accessing it I end up with the default nginx-page from /usr/share/nginx/html instead of being forwarded to my upstream-server

ScuttleSE avatar Aug 10 '17 18:08 ScuttleSE

Does an example.se conf file get created in /etc/nginx/sites-available?

nickjj avatar Aug 10 '17 18:08 nickjj

Indeed it does, a large one

ScuttleSE avatar Aug 10 '17 18:08 ScuttleSE

What happens if you set a root path?

nickjj avatar Aug 10 '17 18:08 nickjj

Well, in the generated conf-file in sites-enabled there is a root set, in this case to root /usr/share/nginx/html;

ScuttleSE avatar Aug 10 '17 18:08 ScuttleSE

Yeah but you can set the root value on your virtual host too (with the path of your site instead).

Example:

  example.se:
    domains: ['example.se', 'www.example.se']
    root: '/path/to/your/root'
    default_server: False
    upstreams:
      - name: 'example'
        servers: ['hostname_of_internal_server:80']

nickjj avatar Aug 10 '17 18:08 nickjj

But since I don't have a root on the nginx-server, what should I set it to? My upstream is on another machine.

ScuttleSE avatar Aug 10 '17 18:08 ScuttleSE

How did you have your nginx config set up before using this role? Paste it here and I'll take a look at it tonight.

nickjj avatar Aug 10 '17 18:08 nickjj

The old config was a huge, ugly monolithic conf, can I email it instead?

ScuttleSE avatar Aug 10 '17 18:08 ScuttleSE

In essence, all the hosts I was running with the old config had this:

location /     {
                  proxy_pass http://upstream_server;
                  proxy_set_header Host $host;
                                }

That was it, no root

ScuttleSE avatar Aug 10 '17 18:08 ScuttleSE

The Problem is the =404 at the end of the try_files. Adding this in group_vars/app.yml fixed the problem for me:

custom_root_location_try_files: '$uri @example'

Note: the @example is generated, it is the name of your upstream entry, if you have multiple add all of them here. This seems to be fixed in https://github.com/nickjj/ansible-nginx/pull/9

carlos22 avatar Jan 26 '18 17:01 carlos22

Thanks. This role is on my radar for getting an overhaul. I will definitely address this.

Going to keep this open until it's merged.

nickjj avatar Jan 26 '18 18:01 nickjj

Yeah, including both an @upstream and =404 in the try_files line doesn't make sense. Only the very last parameter is used as a fallback (http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files)

Also, sometimes it is desirable to proxy all requests to the upstream server (like in @ScuttleSE's snippet) instead of using try_files at all.

I've tried to address both of these issues here: https://github.com/nickjj/ansible-nginx/pull/19

cristoper avatar Feb 11 '18 19:02 cristoper