nginx - 我可以在 Nginx 中创建一个 "private"位置吗?

标签 nginx nginx-location nginx-config auth-request

我可以创建一个可以被 nginx 配置中的任何其他位置访问并且不能从外部直接访问的位置吗?

我可以使用拒绝指令,但它也会拒绝访问 nginx 配置中定义的位置。

这是我的配置 -

server {
  listen *:80;
  server_name 127.0.0.1;

  location = /auth {
      set $query '';
      if ($request_uri ~* "[^\?]+\?(.*)$") {
         set $query $1;
      }
      # add_header X-debug-message "Parameters being passed $is_args$args" always;
      proxy_pass http://127.0.0.1:8080/auth?$query;
  }

  location /kibana/ {
     rewrite ^/kibana/(.*) /$1 break;
     proxy_pass http://127.0.0.1:5601;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection 'upgrade';
     proxy_set_header Host $host;
     proxy_set_header X-Forwarded-For $remote_addr;
     proxy_set_header  X-Real-IP  $remote_addr;
     proxy_cache_bypass $http_upgrade;
     auth_request /auth;
  }

  location ~ (/app/|/app/kibana|/bundles/|/kibana4|/status|/plugins|/ui/|/api/|/monitoring/|/elasticsearch/) {
     internal;
     proxy_pass http://127.0.0.1:5601;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
     proxy_set_header Host $host;
     rewrite /kibana4/(.*)$ /$1 break;
  }

  error_page 404 /404.html;
    location = /40x.html {
  }

  error_page 500 502 503 504 /50x.html;
    location = /50x.html {
  }
}

因此,我需要只能从位置/kibana/访问最后一个位置,但是使用 internal; 它会抛出 404 错误,如果没有它也能正常工作。

我实际上需要用 nginx 保护 kibana,但我最终会在没有任何身份验证的情况下有效地公开它。

最佳答案

你可以使用一个叫做 named location 的东西.它根本无法从外部访问,但在您的配置中,您可以在某些情况下引用它:

location @nginxonly {
    proxy_pass http://example.com/$uri$is_args$args;
}

创建命名位置后,您可以在一些其他地方引用它,例如 try_files 指令中的最后一项。

关于nginx - 我可以在 Nginx 中创建一个 "private"位置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53079801/

相关文章:

php - php-fpm中的pm.max_children是否等于最大并行下载数?

ssl - 在 Sails 上配置 SSL 与在 Nginx 上设置有什么区别?

linux - 如何从 nginx 站点的 URL 中删除端口号

Nginx 速率限制 GET 或 POST 请求仅在一个位置

javascript - 将 .html 附加到我的 href

linux - 如何将.key文件的权限设置为仅由nginx访问

nginx - 结合隐藏文件/目录的nginx规则

c++ - nginx rtmp compile for windows error U1052 & rtmp-module-master/config not found

nginx Content-Security-Policy Header 仍然阻塞 bootstrapcdn