2017-07-11 38 views
2

我在一個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" 
+0

廣東話你剛剛創建2臺虛擬主機要做到這一點 – RiggsFolly

+0

@RiggsFolly我不是太熟悉的nginx,Apache的到來。是否有可能讓兩個虛擬主機偵聽同一個域? –

+0

對不起,我[但我可以使用谷歌](https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/)顯然他們被稱爲服務器塊在nginx – RiggsFolly

回答

1

不清楚自己需要重寫漂亮的URI來什麼,但你需要使用的URI index.php其中包括/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; 
    } 
} 
+0

完美,非常感謝。在Yii2和PrettyURLs啓用的情況下,這可以直接使用。現在API訪問工作,前端不受影響。 –