diff --git a/php-nginx/Dockerfile b/php-nginx/Dockerfile index 557f5edc..b2890927 100644 --- a/php-nginx/Dockerfile +++ b/php-nginx/Dockerfile @@ -82,6 +82,7 @@ RUN mkdir -p $APP_DIR $LOG_DIR $UPLOAD_DIR $SESSION_SAVE_PATH \ # Put config files into place. COPY nginx.conf fastcgi_params gzip_params $NGINX_DIR/conf/ +COPY nginx-app.conf $NGINX_USER_CONF_DIR COPY php.ini $PHP56_DIR/lib/php.ini COPY php.ini $PHP7_DIR/lib/php.ini COPY php-fpm.conf $PHP56_DIR/etc/php-fpm.conf diff --git a/php-nginx/README.md b/php-nginx/README.md index f7b862d8..3a992135 100644 --- a/php-nginx/README.md +++ b/php-nginx/README.md @@ -112,28 +112,35 @@ runtime_config: ## How to change nginx.config -Put `nginx-app.conf` file which includes piece of nginx configuration -for the `server` section in the project top directory. Then it will be -moved to an appropriate directory and included from the main nginx -configuration file. +An `nginx-app.conf` configuration file is included in the server +section of the main nginx configuration file. The default +configuration file looks like this: -Assumption is that you don't often need to entirely modify the -[default nginx file](nginx.conf). -There is an `include` directive in the `server` section and your -configuration file will be included there. +```ini +location / { + # try to serve files directly, fallback to the front controller + try_files $uri /index.php$is_args$args; +} +``` + +To define a custom configuration file, put a file named +`nginx-app.conf` in the project root directory. The runtime will +override the default file with the file you provided. -Here is an example configuration for silex. +By default, index.php is used as the framework front controller. You +may need to change this to something different for your project. The +Symfony framework, for instance, uses app.php instead of index.php. -nginx-app.conf: +Here is an example `nginx-app.conf` for the Symfony framework: ```ini location / { - # try to serve file directly, fallback to front controller - try_files $uri /index.php$is_args$args; + # try to serve files directly, fallback to the front controller + try_files $uri /app.php$is_args$args; } ``` -I hope this mechanism can cover most of the web frameworks, but let us +I hope this mechanism can cover most of your use cases, but let us know if you found otherwise. ## Change the PHP version diff --git a/php-nginx/nginx-app.conf b/php-nginx/nginx-app.conf new file mode 100644 index 00000000..bd637366 --- /dev/null +++ b/php-nginx/nginx-app.conf @@ -0,0 +1,4 @@ +location / { + # try to serve files directly, fallback to the front controller + try_files $uri /index.php$is_args$args; +} diff --git a/tests/ServingTest.php b/tests/ServingTest.php index 77e92c51..f7e024de 100644 --- a/tests/ServingTest.php +++ b/tests/ServingTest.php @@ -53,6 +53,16 @@ public function testIndex() $this->assertContains('Hello World', $resp->getBody()->getContents()); } + public function testNonExistentPath() + { + // Non-existent paths should be served by the `index.php` too, thanks + // to the default nginx-app.conf. + $resp = $this->client->get('non-existent-path'); + $this->assertEquals('200', $resp->getStatusCode(), + 'non-existent-path status code'); + $this->assertContains('Hello World', $resp->getBody()->getContents()); + } + public function testPhpInfo() { // Access to phpinfo.php, while phpinfo() is disabled by default.