nginx 代理从静态虚拟主机传递位置

nginx 代理从静态虚拟主机传递位置

我有一个反向代理(80/443)和一个后端应用程序(8015/44315)。两者均在 nginx 上运行。

反向动态 vhost 工作正常,但我对经典静态 vhost 有点问题......

rp vhost 配置:

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;
  }

}

后端虚拟主机配置:

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;
  }

}

乍一看,一切都应该正常。然而...

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

好的!

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

好的!

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

惊人的!

现在...没有/

$ 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/

为什么没有后面的 / 我会将这个“重定向”到后端端口 8015?我该如何解决?

答案1

它正在执行你的配置中指定的操作 - 这一行

try_files $uri $uri/

未找到文档 /pub,因此它会按照您的要求查找目录 /pub/。

答案2

只需从 proxy_pass 指令中删除尾随斜杠即可。

这个proxy_passhttp://192.168.0.15:8015/

应该是 proxy_passhttp://192.168.0.15:8015

答案3

根据您的建议:

后端虚拟主机配置:

[...]
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 配置:

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

它仍然不起作用

$ 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/

相关内容