Artifactory使用Nginx从网址中删除/ api / gems / gems

时间:2019-07-12 16:24:04

标签: nginx artifactory

我有一个Artifactory服务器作为gem存储库。为了使用本地服务器,我需要使用此丑陋的URL设置我的gem资源。

gem source -a gems.example.com/api/gems/gems/

我想消除开发人员知道URI /api/gems/gems/的需要,因此他们可以简单地访问gems.example.com

server {
  listen       *:443 ssl default;
  server_name   artifactory.example.com gems.example.com;

  ssl on;
  ssl_certificate           /etc/ssl/certs/artifactory.example.com.crt.pem;
  ssl_certificate_key       /etc/ssl/private/artifactory.example.com.key.pem;
  location / {
    if ( $request_uri ~ ^/(.*)$ ) { proxy_pass http://localhost:8081/artifactory/$1; }
    proxy_cookie_path ~*^/.* /;
    proxy_pass http://localhost:8081/artifactory/;
    proxy_pass_header Server;
    proxy_read_timeout 2400s;
  }

可以进行重写,但是nginx配置已经具有以下if,并且nginx不允许多个条件。

无效:

if ( $request_uri ~ ^/(.*)$ && $server_name) { proxy_pass http://localhost:8081/artifactory/$1; }

如何从网址/api/gems/gems/中删除https://gems.example.com/api/gems/gems/

1 个答案:

答案 0 :(得分:0)

使用多个服务器块,如下所示:

请注意,第二个服务器块具有以下更改。
-server_name
-监听行上没有“默认”
-proxy_pass http://localhost:8081/artifactory/api/gems/gems/

/etc/nginx/sites-enabled/artifactory.conf

server {
  listen       *:443 ssl default;
  server_name   artifactory.example.com;

  ssl on;
  ssl_certificate           /etc/ssl/certs/artifactory.example.com.crt.pem;
  ssl_certificate_key       /etc/ssl/private/artifactory.example.com.key.pem;
  location / {
    if ( $request_uri ~ ^/(.*)$ ) { proxy_pass http://localhost:8081/artifactory/$1; }
    proxy_cookie_path ~*^/.* /;
    proxy_pass http://localhost:8081/artifactory/;
    proxy_pass_header Server;
    proxy_read_timeout 2400s;
  }
  # ....

/etc/nginx/sites-enabled/artifactory-gems.conf

server {
  listen       *:443 ssl;
  server_name   gems.example.com;

  ssl on;
  ssl_certificate           /etc/ssl/certs/artifactory.example.com.crt.pem;
  ssl_certificate_key       /etc/ssl/private/artifactory.example.com.key.pem;
  location / {
    if ( $request_uri ~ ^/(.*)$ ) { proxy_pass http://localhost:8081/artifactory/api/gems/gems/$1; }
    proxy_cookie_path ~*^/.* /;
    proxy_pass http://localhost:8081/artifactory/api/gems/gems/;
    proxy_pass_header Server;
    proxy_read_timeout 2400s;
  }
  # ....

注释/gems/gems对应于工件上称为“宝石”的虚拟仓库。如果您有多个gem储存库,则需要为每个存储库添加一个虚拟主机。