两个文档根与nginx和fastcgi

我在docker环境中使用nginx和php,对于单个应用程序来说工作正常。 现在我想开发一个基于yii2 / php的应用程序,作为后端和angular作为前端,所以我需要一个web服务器为客户端服务,另一个服务于API后端。 目录结构如下所示:

/var/www/html/ $ tree -L 3 . ├── client │  ├── dist │  │  ├── 0.chunk.js │  │  ├── 0.chunk.js.map │  │  ├── assets │  │  ├── index.html │  │  ├── ... │  ├── e2e │  │  ├── ... │  ├── node_modules │  │  ├── ... ├── docker │  ├── mysql │  │  ├── Dockerfile │  │  └── my.cnf │  ├── nginx │  │  ├── Dockerfile │  │  └── default.conf │  └── php7 │  └── Dockerfile ├── docker-compose.yml └── server ├── api │  ├── common │  ├── config │  ├── modules │  └── web │ │  └── index.php ├── common ├── composer.json ├── console └── vendor 

前端应用程序位于`/ var / www / html / client / dist /中,nginxconfiguration如下所示:

 server { listen 80 default_server; root /var/www/html/client/dist/; index index.html index.php; charset utf-8; location / { # Redirect everything that isn't a real file to index.php try_files $uri $uri/ /index.php$is_args$args; } location /api { root /var/www/html/server/api/web/; try_files $uri $uri/ /index.php$is_args$args; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } sendfile off; client_max_body_size 100m; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; fastcgi_read_timeout 300s; } location ~ /\.ht { deny all; } } 

使用这个configuration,前端工作(URL:/),但是API不。 我需要的是:

请求“/”:从/var/www/html/client/dist/ Request服务angular度应用“/ api”:使用index.php/var/www/html/server/api/web/

如何configuration这个? 谢谢。

//编辑:新的configuration文件:

 server { listen 80 default_server; root /var/www/html/client/dist/; index index.html; charset utf-8; location ~ ^/api(.*) { alias /var/www/html/server/api/web/; # Redirect everything that isn't a real file to index.php try_files $uri $uri/ /index.php$1$args; index index.php; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; fastcgi_read_timeout 300s; } location ~ /\.ht { deny all; } } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log off; error_log /var/log/nginx/error.log error; sendfile off; client_max_body_size 100m; } 

调用http:// localhost / api / v1 / users应该被redirect到/var/www/html/server/api/web/index.php,以v1 / users作为参数(或者Yii2处理漂亮的URL),但是返回一个404没有find。

错误日志显示此消息,所以它看起来像别名指令没有生效:

 2017/07/11 16:51:57 [error] 5#5: *1 open() "/var/www/html/client/dist/index.php" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /api/v1/users HTTP/1.1", host: "localhost" 

不完全确定你需要重写那些漂亮的URI,但是你需要使用包含/api前缀的index.php的URI。

alias指令对前缀location效果最好,否则需要用捕获的变量构建路径。

例如:

 location ^~ /api/ { alias /var/www/html/server/api/web/; index index.php; if (!-e $request_filename) { rewrite ^/api(.*) /api/index.php?uri=$1 last; } location ~ \.php$ { if (!-f $request_filename) { return 404; } fastcgi_pass php:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; fastcgi_read_timeout 300s; } location ~ /\.ht { deny all; } }