2017-07-11 4 views
2

Ich benutze Nginx und PHP in einer Docker-Umgebung, die für eine einzelne Anwendung funktioniert. Jetzt möchte ich eine Anwendung entwickeln, die auf yii2/php als Backend und eckig als Frontend basiert, also brauche ich einen Webserver, der den Client bedient, und einen anderen, der das API-Backend bedient. Die Verzeichnisstruktur sieht wie folgt aus:Zwei Dokumentwurzeln mit Nginx und 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 

Die Frontend-Anwendung in `/ var/www/html/client/dist/befindet, sucht der nginx Config wie folgt:

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; 
    } 
} 

diese Config funktioniert das Frontend (URL: /), die API jedoch nicht. Was ich brauche, ist:

Request "/": dienen die Winkel App von /var/www/html/client/dist/ Request "/ api": Verwenden Sie index.php von /var/www/html/server/api/web/

Wie dies konfigurieren? Vielen Dank.

// Edit: Neue Konfigurationsdatei:

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 aufrufen sollte mit v1/Benutzer als Parameter (oder aber /var/www/html/server/api/web/index.php umgeleitet werden Yii2 behandelt die hübschen URLs), gibt aber stattdessen 404 zurück.

Fehlerprotokoll zeigt diese Meldung, so dass es wie die Alias-Richtlinie sieht nicht greift:

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

Cant Sie schaffen nur 2 Virtual Hosts diese – RiggsFolly

+0

@RiggsFolly zu tun, ich bin nicht allzu vertraut mit nginx, von Apache kommt. Ist es möglich, dass zwei virtuelle Hosts nach der gleichen Domain suchen? –

+0

Entschuldigung, bin ich nicht, aber [ich kann Google verwenden] (https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/) Offenbar werden sie Server Blöcke in nginx – RiggsFolly

Antwort

1

Nicht ganz sicher, was Sie die hübschen URIs, neu zu schreiben müssen, aber Sie müssen den URI verwenden index.php, die das Präfix /api enthält.

Die alias directive funktioniert am besten mit einer prefix location, andernfalls müssen Sie den Pfad mit erfassten Variablen erstellen.

Zum Beispiel:

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

Perfekt, vielen Dank. Dies funktioniert sofort, wenn Yii2 und PrettyURLs aktiviert sind. API-Zugriff funktioniert jetzt und Frontend ist nicht betroffen. –

Verwandte Themen