From b5fd61cfb95318669b4d7fb315238e8e727076df Mon Sep 17 00:00:00 2001 From: cytopia Date: Tue, 18 Dec 2018 12:30:57 +0100 Subject: [PATCH] Ensure PHP-FPM works exactly as for Apache on main vhost --- Dockerfile | 1 + Makefile | 2 +- README.md | 2 +- data/docker-entrypoint.d/07-vhost-gen.sh | 4 +- data/vhost-gen/templates-main/apache22.yml | 0 data/vhost-gen/templates-main/apache24.yml | 0 data/vhost-gen/templates-main/nginx.yml | 173 +++++++++++++++++++++ 7 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 data/vhost-gen/templates-main/apache22.yml create mode 100644 data/vhost-gen/templates-main/apache24.yml create mode 100644 data/vhost-gen/templates-main/nginx.yml diff --git a/Dockerfile b/Dockerfile index dcc9273..fb986f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -88,6 +88,7 @@ RUN set -x \ ### COPY ./data/vhost-gen/main.yml /etc/vhost-gen/main.yml COPY ./data/vhost-gen/mass.yml /etc/vhost-gen/mass.yml +COPY ./data/vhost-gen/templates-main /etc/vhost-gen/templates-main COPY ./data/create-vhost.sh /usr/local/bin/create-vhost.sh COPY ./data/docker-entrypoint.d /docker-entrypoint.d COPY ./data/docker-entrypoint.sh /docker-entrypoint.sh diff --git a/Makefile b/Makefile index b075c6d..0361331 100644 --- a/Makefile +++ b/Makefile @@ -21,4 +21,4 @@ test: .ci/start-ci.sh $(image) $(ARG) pull: - docker pull $(shell grep FROM Dockerfile | sed 's/^FROM//g'; done) + docker pull $(shell grep 'FROM' Dockerfile | sed 's/^FROM//g'; ) diff --git a/README.md b/README.md index 047d5bb..3886393 100644 --- a/README.md +++ b/README.md @@ -257,5 +257,5 @@ It allows any of the following combinations: ## Version ``` -nginx version: nginx/1.14.0 +nginx version: nginx/1.14.2 ``` diff --git a/data/docker-entrypoint.d/07-vhost-gen.sh b/data/docker-entrypoint.d/07-vhost-gen.sh index 44f22ac..30f6f5d 100755 --- a/data/docker-entrypoint.d/07-vhost-gen.sh +++ b/data/docker-entrypoint.d/07-vhost-gen.sh @@ -92,7 +92,9 @@ vhost_gen_generate_main_vhost() { else verbose="" fi - run "vhost_gen.py -n localhost -p ${docroot} -c ${config} -o ${template} ${verbose} -d -s -m ${ssl_type}" "${debug}" + # Adding custom nginx vhost template to ensure paths like: + # /vendor/index.php/arg1/arg2 will also work (just like Apache) + run "vhost_gen.py -n localhost -p ${docroot} -t /etc/vhost-gen/templates-main/ -c ${config} -o ${template} ${verbose} -d -s -m ${ssl_type}" "${debug}" fi } diff --git a/data/vhost-gen/templates-main/apache22.yml b/data/vhost-gen/templates-main/apache22.yml new file mode 100644 index 0000000..e69de29 diff --git a/data/vhost-gen/templates-main/apache24.yml b/data/vhost-gen/templates-main/apache24.yml new file mode 100644 index 0000000..e69de29 diff --git a/data/vhost-gen/templates-main/nginx.yml b/data/vhost-gen/templates-main/nginx.yml new file mode 100644 index 0000000..43f3525 --- /dev/null +++ b/data/vhost-gen/templates-main/nginx.yml @@ -0,0 +1,173 @@ +--- + +# +# Slightly modified version of the nginx template for mass vhost +# This mitigates the following issue: +# https://www.reddit.com/r/nginx/comments/a6pw31/phpfpm_does_not_handle_subpathindexphparg1arg2/ +# +# Search this file for 'CHANGED:' +# + + +# Nginx vHost Template defintion for vhost-gen.py +# +# The 'feature' section contains optional features that can be enabled via +# conf.yml and will then be replaced into the main vhost ('structure' section) +# into their corresponding position: +# +# __XDOMAIN_REQ__ +# __PHP_FPM__ +# __ALIASES__ +# __DENIES__ +# __STATUS__ +# +# The features itself also contain variables to be adjusted in conf.yml +# and will then be replaced in their corresponding feature section +# before being replaced into the vhost section (if enabled): +# +# PHP-FPM: +# __PHP_ADDR__ +# __PHP_PORT__ +# XDomain: +# __REGEX__ +# Alias: +# __REGEX__ +# __PATH__ +# Deny: +# __REGEX__ +# Status: +# __REGEX__ +# +# Variables to be replaced directly in the vhost configuration can also be set +# in conf.yml and include: +# __VHOST_NAME__ +# __DOCUMENT_ROOT__ +# __INDEX__ +# __ACCESS_LOG__ +# __ERROR_LOG__ +# __PHP_ADDR__ +# __PHP_PORT__ +# + + +### +### Basic vHost skeleton +### +vhost: | + server { + listen __PORT____DEFAULT_VHOST__; + server_name __VHOST_NAME__; + + access_log "__ACCESS_LOG__" combined; + error_log "__ERROR_LOG__" warn; + + __REDIRECT__ + __SSL__ + __VHOST_DOCROOT__ + __VHOST_RPROXY__ + __PHP_FPM__ + __ALIASES__ + __DENIES__ + __SERVER_STATUS__ + # Custom directives + __CUSTOM__ + } + + +### +### vHost Type (normal or reverse proxy) +### +vhost_type: + # Normal vHost (-p) + docroot: | + # Define the vhost to serve files + root "__DOCUMENT_ROOT__"; + index __INDEX__; + + # Reverse Proxy (-r) + rproxy: | + # Define the vhost to reverse proxy + location __LOCATION__ { + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_pass __PROXY_PROTO__://__PROXY_ADDR__:__PROXY_PORT__; + } + + +### +### Optional features to be enabled in vHost +### +features: + + # SSL Configuration + ssl: | + ssl_certificate __SSL_PATH_CRT__; + ssl_certificate_key __SSL_PATH_KEY__; + ssl_protocols __SSL_PROTOCOLS__; + ssl_prefer_server_ciphers __SSL_HONOR_CIPHER_ORDER__; + ssl_ciphers __SSL_CIPHERS__; + + # Redirect to SSL directive + redirect: | + return 301 https://__VHOST_NAME__:__SSL_PORT__$request_uri; + + # PHP-FPM will not be applied to a reverse proxy! + php_fpm: | + # PHP-FPM Definition + location / { + try_files $uri $uri/ /index.php$is_args$args; + } + # CHANGED: + #location ~ \.php?$ { + location ~ \.php($|/) { + set $script $uri; + if ($uri ~ "^(.+\.php)(/.+)") { + set $script $1; + } + # end of CHANGED: + try_files $uri = 404; + include fastcgi_params; + + # https://stackoverflow.com/questions/1733306/nginx-errors-readv-and-recv-failed/51457613#51457613 + fastcgi_keep_conn off; + + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_split_path_info ^(.+\.php)(.*)$; + + fastcgi_pass __PHP_ADDR__:__PHP_PORT__; + fastcgi_read_timeout __PHP_TIMEOUT__; + + fastcgi_index index.php; + fastcgi_intercept_errors on; + } + + alias: | + # Alias Definition + location ~ __ALIAS__ { + root __PATH__; + __XDOMAIN_REQ__ + } + + deny: | + # Deny Definition + location ~ __REGEX__ { + deny all; + } + + server_status: | + # Status Page + location ~ __REGEX__ { + stub_status on; + access_log off; + } + + xdomain_request: | + # Allow cross domain request from these hosts + if ( $http_origin ~* (__REGEX__) ) { + add_header "Access-Control-Allow-Origin" "$http_origin"; + add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; + add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; + add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; + add_header 'Access-Control-Max-Age' 0; + return 200; + }