Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Provide a default nginx-app.conf #139

Merged
merged 2 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions php-nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 12 additions & 14 deletions php-nginx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,28 +112,26 @@ 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.
We put `nginx-app.conf` file which is included in the `server` section
Copy link
Contributor

@bshaffer bshaffer Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hard to read. How about this?

An nginx-app.conf configuration file is included in the server section of the main configuration file. The default configuration file looks like this:
//...
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.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

of the main configuration file. Here is the default configuration file
which will likely just work with most of the modern web frameworks in
simple cases.

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.

Here is an example configuration for silex.

nginx-app.conf:
The default nginx-app.conf:

```ini
location / {
# try to serve file directly, fallback to front controller
# try to serve files directly, fallback to the front controller
try_files $uri /index.php$is_args$args;
}
```

I hope this mechanism can cover most of the web frameworks, but let us
You might need to add some rewrite rules, or you might want to change
the behavior. In those cases, put a file named `nginx-app.conf` in the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symfony uses app.php in the root of the project as a convention, so you should also add instructions on how to do this:

In this case, 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to the suggested edit above.

project root directory. Then the runtime will override the default
file with the file you provided.

I hope this mechanism can cover most of your use cases, but let us
know if you found otherwise.

## Change the PHP version
Expand Down
4 changes: 4 additions & 0 deletions php-nginx/nginx-app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
location / {
# try to serve files directly, fallback to the front controller
try_files $uri /index.php$is_args$args;
}
10 changes: 10 additions & 0 deletions tests/ServingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down