Skip to content

Commit

Permalink
Update nginx reverse proxy config to avoid serving PHP files
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Mar 5, 2024
1 parent a3fffcf commit 9663ee7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
26 changes: 25 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,31 @@ jobs:
- run: docker logs $(docker ps -qn1)
if: ${{ always() }}

nginx-webserver:
nginx-reverse-proxy:
name: nginx reverse proxy + X
runs-on: ubuntu-22.04
strategy:
matrix:
config:
- nginx-reverse-proxy-public.conf
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.3
- run: composer install -d tests/integration/
- run: docker build -f tests/integration/Dockerfile-basics tests/integration/
- run: docker run -d -p 8080:8080 -v "$PWD/composer.json":/app/composer.json $(docker images -q | head -n1)
- run: docker run --net=host -v "$PWD/tests/integration/":/home/framework-x/ -v "$PWD"/tests/integration/${{ matrix.config }}:/etc/nginx/conf.d/default.conf nginx:stable-alpine
- run: bash tests/await.sh http://localhost
- run: bash tests/integration.bash http://localhost
- run: docker stop $(docker ps -qn2)
- run: docker logs $(docker ps -qn1)
if: ${{ always() }}
- run: docker logs $(docker ps -qn2 | tail -n1)
if: ${{ always() }}

nginx-fpm:
name: nginx + PHP-FPM (PHP ${{ matrix.php }})
runs-on: ubuntu-22.04
strategy:
Expand Down
17 changes: 13 additions & 4 deletions docs/best-practices/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,24 @@ achieved by using an nginx configuration with the following contents:

```
server {
root /home/alice/projects/acme/public;
index index.php index.html;
# Serve static files from `public/`, proxy dynamic requests to Framework X
location / {
try_files $uri $uri/ @x;
location ~* \.php$ {
try_files /dev/null @x;
}
root /home/alice/projects/acme/public;
try_files $uri @x;
}
location @x {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header Connection "";
}
# Optional: handle Apache config with Framework X if it exists in `public/` directory
location ~ \.htaccess$ {
try_files /dev/null @x;
}
}
```
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/nginx-reverse-proxy-public.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server {
# Serve static files from `public/`, proxy dynamic requests to Framework X
location / {
location ~* \.php$ {
try_files /dev/null @x;
}
root /home/framework-x/public;
try_files $uri @x;
}

location @x {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header Connection "";
}

# Optional: handle Apache config with Framework X if it exists in `public/` directory
location ~ \.htaccess$ {
try_files /dev/null @x;
}
}

0 comments on commit 9663ee7

Please sign in to comment.