我在docker-environment中使用nginx和php,它适用于单个应用程序。现在我想开发一个基于yii2 / php的应用程序作为后端,角度作为前端,所以我需要一个服务于客户端的Web服务器,另一个服务于API后端。目录结构如下所示:
let imageView = UIImageView( image: UIImage( named: "logo.png" ) )
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
let height = NSLayoutConstraint(
item: imageView, attribute: .height,
relatedBy: .equal,
toItem: self.navigationItem.titleView, attribute: .height,
multiplier: 1, constant: 0
)
NSLayoutConstraint.activate( [ height ] )
前端应用程序位于`/ var / www / html / client / dist /中,nginx配置如下所示:
/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
使用此配置,前端工作(URL:/),但API不工作。我需要的是:
请求“/”:从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;
}
}
投放角度应用
请求“/ api”:使用/var/www/html/client/dist/
index.php
如何配置?谢谢。
//编辑:新配置文件:
/var/www/html/server/api/web/
调用http://localhost/api/v1/users应该重定向到/var/www/html/server/api/web/index.php,其中v1 / users作为参数(或者Yii2处理漂亮的URL),但是不返回404找到了。
错误日志显示此消息,因此看起来alias指令未生效:
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;
}
答案 0 :(得分:1)
不完全确定您需要重写漂亮的URI,但是您需要使用index.php
的URI,其中包含/api
前缀。
alias
directive最适合prefix 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;
}
}