【问题标题】:NGINX Config File for Microservice Subfolder Architecture微服务子文件夹架构的 NGINX 配置文件
【发布时间】:2020-04-02 23:34:09
【问题描述】:

请多多包涵,因为我没有太多配置 Nginx 的经验。

我想将微服务创建为子文件夹。例如:www.example.com/users

USERS 是微服务,因此 USERS 也是子文件夹的名称。

该网站可以托管在 /var/www/html/ 或 /var/www/html/example.com/public_html (假设这个问题用于此问题)

所以在主文件夹 /var/www/html/example.com/public_html 里面有很多文件夹,每个文件夹代表一个微服务。

/var/www/html/example.com/public_html/users/
/var/www/html/example.com/public_html/students/
/var/www/html/example.com/public_html/teachers/
/var/www/html/example.com/public_html/parents/

每个微服务都会有一个最新的文件夹来保存要执行的代码。

/var/www/html/example.com/public_html/users/latest/

每个最新文件夹都有 2 个文件夹 - 包含 SPA 前端代码的 APP 和包含 API 代码的 API

/var/www/html/example.com/public_html/users/latest/app/
/var/www/html/example.com/public_html/users/latest/api/

我想在不更改 Nginx 配置文件的情况下根据需要添加和删除文件夹。 如果该文件夹存在,我希望显示相关的前端代码。否则 404

我知道我可以根据需要在 Nginx 位置添加文件夹,但这不是我想要的。

(不想执行以下代码)

location /users {
  alias /var/www/html/example.com/public_html/users/latest/app
}

location /api/users {
  alias /var/www/html/example.com/public_html/users/latest/api
}

我正在尝试确定是否可以将 NGINX 配置为基于 url 指向相关文件夹。

这是我到目前为止所拥有的,但对于我正在尝试的所有内容,它只显示为 404。我正在努力用 Go-lang 编写 API,但由于我有使用 PHP 的经验,所以我在切换到 Go 之前先走这条路。

server {

    listen 80;
    listen [::]:80;
    server_name _;

    root /var/www/html/example.com/public_html;
    index index.html;

    location ^/<service>/<additional-optional-parameters-can-go-here>$ {
      root /var/www/html/example.com/public_html/<service>/latest/app;
      try_files $uri $uri/ =404
    }

    location ^/api/<service>/<additional-optional-parameters-can-go-here>$ {
      root /var/www/html/example.com/public_html/<service>/latest/api;
      try_files $uri $uri/ =404

      location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

    }

    location ~ /\.ht {
        deny all;
    }

}

【问题讨论】:

    标签: vue.js nginx microservices single-page-application nginx-location


    【解决方案1】:

    我认为您可以在 location 和 server 行中使用正则表达式。

    也许会有所帮助

    server_name ~^(?<subdomain>.+)\.domain\.tdl$ ~^(?<subdomain>.+)\.domain2\.tdl$ domain.tdl domain2.tdl;
    
    location ~ ^/sitename/[0-9a-z]+/index.php$ {
        fastcgi_pass phpcgi;
    }
    
    location ~ \.php$ {
        return 404;
    }
    
    location ~ ^/a/b/(?<myvar>[a-zA-Z]+) {
       # use variable $myvar here
       if ($myvar = "sth") { ... }
    }
    
    # Example using the subdomain var created in last steps
    sub_filter_once off;
    sub_filter 'Site Text' '$subdomain';
    sub_filter 'Site Text' '$myvar';
    

    我个人喜欢为每个服务创建不同的 conf 文件,然后发出“service nginx reload”。也许您还可以自动生成 conf 文件。

    【讨论】:

    • 我会尝试这种方法并在明天之前回复。
    • 那行不通。我只是一直转到根文件夹或基本文件夹。
    • 感谢 Rahim,这让我朝着正确的方向前进。
    【解决方案2】:

    以防万一其他人正在尝试类似的方法。 我去了 Freelancer.com 并雇了人来帮助我。

    location ~ ^/api/([a-z]+)/.*?$ {
        alias $folder_path/$1/$api_path;
        rewrite ^/api/([a-z]+)/.*?$ /$1/$api_path last;
    }
    
    location ~ ^/([a-z]+)/(?:(?!\blatest\b).)*$ {
        alias $folder_path/$1/$app_path;
        rewrite ^/([a-z]+)/(?:(?!\blatest\b).)*$ /$1/$app_path last;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 2020-01-06
      • 2017-07-15
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多