nickjj / ansible-nginx

Install and configure nginx (SSL A+ by default) with Ansible.
MIT License
73 stars 37 forks source link

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

Open ScuttleSE opened 7 years ago

ScuttleSE commented 7 years ago

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

nickjj commented 7 years ago

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

ScuttleSE commented 7 years ago

Indeed it does, a large one

nickjj commented 7 years ago

What happens if you set a root path?

ScuttleSE commented 7 years ago

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

nickjj commented 7 years ago

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']
ScuttleSE commented 7 years ago

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

nickjj commented 7 years ago

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.

ScuttleSE commented 7 years ago

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

ScuttleSE commented 7 years ago

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

carlos22 commented 6 years ago

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

nickjj commented 6 years ago

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.

cristoper commented 6 years ago

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