我在一个docker环境中使用nginx和php,它对单个应用程序工作正常。现在我想开发一个基于yii2/php的应用程序作为后端和angular作为前端,所以我需要一个webserver服务于客户端,另一个服务于API后端。目录结构如下所示:nginx和fastcgi的两个文档根
/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等/客户/距离/,Nginx的配置如下所示:
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;
}
}
使用该配置,前端工作(URL:/),但是API不。我需要的是:
请求 “/”:从/var/www/html/client/dist/
请求 “/ API” 服务的角度应用:使用来自/var/www/html/server/api/web/
index.php
如何配置呢?谢谢。
//编辑:新的配置文件:
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应该转用V1 /用户作为参数传递给/var/www/html/server/api/web/index.php(或然Yii2处理漂亮的URL),但是返回404找不到。
错误日志显示了该消息,所以它看起来像别名指令不发生作用:
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"
广东话你刚刚创建2台虚拟主机要做到这一点 – RiggsFolly
@RiggsFolly我不是太熟悉的nginx,Apache的到来。是否有可能让两个虚拟主机侦听同一个域? –
对不起,我[但我可以使用谷歌](https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/)显然他们被称为服务器块在nginx – RiggsFolly