0

I have a reverse proxy (80/443) and a back-end application (8015/44315). Both run on nginx.

The reverse dynamic vhost works fine but I have an little issue with a classic static vhost...

rp vhost conf:

server {
  listen 443 ssl;
  server_name domain.tld;

  ssl_certificate /usr/local/etc/letsencrypt/live/domain.tld/fullchain.pem;
  ssl_certificate_key /usr/local/etc/letsencrypt/live/domain.tld/privkey.pem;

  return 301 https://www.$host$request_uri$is_args$query_string;
}

server {
  listen 443 ssl;
  server_name  www.domain.tld;

  ssl_certificate /usr/local/etc/letsencrypt/live/domain.tld/fullchain.pem;
  ssl_certificate_key /usr/local/etc/letsencrypt/live/domain.tld/privkey.pem;

  access_log /var/log/nginx/access_domain.tld.log combined;
  error_log  /var/log/nginx/error_domain.tld.log;

  location / {
    proxy_pass http://192.168.0.15:8015/;
    proxy_redirect default;
    proxy_set_header Host $host;
  }

}

backend vhost conf:

server {
  listen       8015;                                  
  server_name  www.domain.tld;

  root /usr/local/www/domain;

  access_log /var/log/nginx/access_domain.tld.log combined;
  error_log /var/log/nginx/error_domain.tld.log;

  include /usr/local/etc/nginx/snippets/generic.conf;

  location / {
    alias /usr/local/www/nginx/;
    try_files $uri $uri/ /index.html;
    index index.html index.htm;
  }

  location /pub {
    alias /usr/local/www/domain/pub;
    allow all;
    autoindex off;
  }

  location /static {
    alias /usr/local/www/domain/static;
    autoindex on;
    fancyindex on;
    fancyindex_exact_size off;
  }

  location /vid {
    alias /usr/local/www/domain/vid;
    autoindex on;
    fancyindex on;
    fancyindex_exact_size off;
    allow all;
  }

}

First look, everything should be works. However...

$ curl -I https://www.domain.tld/ 
HTTP/1.1 200 OK

Ok!

$ curl -I https://www.domain.tld/pub/
HTTP/1.1 403 Forbidden

OK!

$ curl -I https://www.domain.tld/pub/file.txt
HTTP/1.1 200 OK

Awesome!

And now... without /

$ curl -I https://www.domain.tld/pub
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sun, 20 Nov 2016 19:04:03 GMT
Content-Type: text/html
Connection: keep-alive
Location: http://www.domain.tld:8015/pub/

Why without the trailing / I have this "redirection" to backend port 8015? How can I solve that ?

Tim
  • 31,888
  • 7
  • 52
  • 78
pstk
  • 1
  • 2

3 Answers3

0

It's doing what's specified in your configuration - this line

try_files $uri $uri/

The document /pub wasn't found so it looks for the directory /pub/ as you've told it to.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • I see.. But I have commented this line and I have the same result :\ Thanks – pstk Nov 20 '16 at 19:52
  • I assume you restarted Nginx? Don't just comment the line out, replace it with what you need - "try_files $uri try_files $uri $uri/ /index.html;" in case there's a default in place. – Tim Nov 20 '16 at 20:09
  • Of course. I've tried this : location / { alias /usr/local/www/nginx/; try_files $uri try_files $uri $uri/ /index.html; index index.html index.htm; } The issue could not be come from the reverse proxy configuration instead of the backend ? – pstk Nov 20 '16 at 22:37
  • I said that because if I had: location /pub { proxy_pass http://192.168.0.15:8015/pub/; proxy_redirect default; proxy_set_header Host $host; } $ curl -I https://www.domain.tld/pub HTTP/1.1 403 Forbidden It works. – pstk Nov 20 '16 at 22:50
  • Too difficult to read code in comments, please edit your question to add everything you've tried and why it didn't work. Your comment starting "of course" still has the $uri/ in it. – Tim Nov 21 '16 at 00:56
0

Just remove the trailing slash from proxy_pass directive.

This proxy_pass http://192.168.0.15:8015/

Should be proxy_pass http://192.168.0.15:8015

x86fantini
  • 302
  • 1
  • 3
  • 9
0

After your suggestions:

backend vhost conf:

[...]
location / {
  alias /usr/local/www/nginx/;
  try_files $uri try_files $uri $uri/ /index.html;
  index index.html index.htm;
}

location /pub {
  alias /usr/local/www/domain/pub;
  allow all;
  autoindex off;
}
[...]

rp vhost conf:

[..]
location / {
  proxy_pass http://192.168.0.15:8015;
  proxy_redirect default;
  proxy_set_header Host $host;
}
[..]

It still does not works

$ curl -I https://www.domain.tld/pub/
HTTP/1.1 403 Forbidden

$ curl -I https://www.domain.tld/pub
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 21 Nov 2016 08:04:03 GMT
Content-Type: text/html
Connection: keep-alive
Location: http://www.domain.tld:8015/pub/
pstk
  • 1
  • 2