From 3a6a46584dc0c8a9285562e14c5673b73d8b01f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Constantin=20Wildf=C3=B6rster?= Date: Tue, 4 Feb 2020 20:00:01 +0100 Subject: [PATCH] improve seperate log variable check --- nginx.tmpl | 24 ++++++++++++---------- test/test_virtual_path.py | 23 +++++++++++++++++++++ test/test_virtual_path.yml | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 test/test_virtual_path.py create mode 100644 test/test_virtual_path.yml diff --git a/nginx.tmpl b/nginx.tmpl index a137e3f..eabfa1e 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -107,6 +107,8 @@ map $scheme $proxy_x_forwarded_ssl { gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; +{{ $seperate_logFiles := eq (or ($.Env.SEPERATE_LOGS_PER_VHOST) "") "true" }} + log_format vhost '$host $remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"'; @@ -257,7 +259,7 @@ server { listen [::]:{{ $external_http_port }} {{ $default_server }}; {{ end }} - {{ if $.Env.SEPERATE_LOGS_PER_VHOST }} + {{ if $seperate_logFiles }} access_log /var/log/nginx/{{ $host }}.access.log; error_log /var/log/nginx/{{ $host }}.error.log warn; {{ else }} @@ -286,7 +288,7 @@ server { listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }}; {{ end }} - {{ if $.Env.SEPERATE_LOGS_PER_VHOST }} + {{ if $seperate_logFiles }} access_log /var/log/nginx/{{ $host }}.access.log; error_log /var/log/nginx/{{ $host }}.error.log warn; {{ else }} @@ -389,7 +391,7 @@ server { listen [::]:80 {{ $default_server }}; {{ end }} - {{ if $.Env.SEPERATE_LOGS_PER_VHOST }} + {{ if $seperate_logFiles }} access_log /var/log/nginx/{{ $host }}.access.log; error_log /var/log/nginx/{{ $host }}.error.log warn; {{ else }} @@ -468,11 +470,11 @@ server { listen [::]:{{ $external_https_port }} ssl http2 {{ $default_server }}; {{ end }} - {{ if $.Env.SEPERATE_LOGS_PER_VHOST }} - access_log /var/log/nginx/{{ $host }}.access.log; - error_log /var/log/nginx/{{ $host }}.error.log warn; + {{ if $seperate_logFiles }} + access_log /var/log/nginx/{{ $host }}.access.log; + error_log /var/log/nginx/{{ $host }}.error.log warn; {{ else }} - access_log /var/log/nginx/access.log vhost; + access_log /var/log/nginx/access.log vhost; {{ end }} return 500; @@ -537,7 +539,7 @@ server { listen [::]:80 {{ $default_server }}; {{ end }} - {{ if $.Env.SEPERATE_LOGS_PER_VHOST }} + {{ if $seperate_logFiles }} access_log /var/log/nginx/{{ $host_alias }}.access.log; error_log /var/log/nginx/{{ $host_alias }}.error.log warn; {{ else }} @@ -554,7 +556,7 @@ server { listen [::]:443 ssl http2 {{ $default_server }}; {{ end }} - {{ if $.Env.SEPERATE_LOGS_PER_VHOST }} + {{ if $seperate_logFiles }} access_log /var/log/nginx/{{ $host_alias }}.access.log; error_log /var/log/nginx/{{ $host_alias }}.error.log warn; {{ else }} @@ -604,7 +606,7 @@ server { listen [::]:80 {{ $default_server }}; {{ end }} - {{ if $.Env.SEPERATE_LOGS_PER_VHOST }} + {{ if $seperate_logFiles }} access_log /var/log/nginx/{{ $host_alias }}.access.log; error_log /var/log/nginx/{{ $host_alias }}.error.log warn; {{ else }} @@ -633,7 +635,7 @@ server { listen [::]:443 ssl http2 {{ $default_server }}; {{ end }} - {{ if $.Env.SEPERATE_LOGS_PER_VHOST }} + {{ if $seperate_logFiles }} access_log /var/log/nginx/{{ $host_alias }}.access.log; error_log /var/log/nginx/{{ $host_alias }}.error.log warn; {{ else }} diff --git a/test/test_virtual_path.py b/test/test_virtual_path.py new file mode 100644 index 0000000..81f7b3f --- /dev/null +++ b/test/test_virtual_path.py @@ -0,0 +1,23 @@ +import pytest + + +def test_default(docker_compose, nginxproxy): + r = nginxproxy.get("http://nginx-proxy.test/port") + assert r.status_code == 200 + assert r.text == "answer from port 81\n" + +def test_simple_path(docker_compose, nginxproxy): + r = nginxproxy.get("http://nginx-proxy.test/foo/port") + assert r.status_code == 200 + assert r.text == "answer from port 82\n" + +def test_deep_path(docker_compose, nginxproxy): + r = nginxproxy.get("http://nginx-proxy.test/bar/even/deeper/port") + assert r.status_code == 200 + assert r.text == "answer from port 83\n" + +def test_closed_path(docker_compose, nginxproxy): + r = nginxproxy.get("http://nginx-proxy.test/bar/even/deeper/with/end/port") + assert r.status_code == 200 + assert r.text == "answer from port 84\n" + diff --git a/test/test_virtual_path.yml b/test/test_virtual_path.yml new file mode 100644 index 0000000..205ff00 --- /dev/null +++ b/test/test_virtual_path.yml @@ -0,0 +1,41 @@ +web1: + image: web + expose: + - "81" + environment: + WEB_PORTS: "81" + VIRTUAL_HOST: "nginx-proxy.test" + +web2: + image: web + expose: + - "82" + environment: + WEB_PORTS: "82" + VIRTUAL_HOST: "nginx-proxy.test" + VIRTUAL_PATH: "/foo" + +web3: + image: web + expose: + - "83" + environment: + WEB_PORTS: "83" + VIRTUAL_HOST: "nginx-proxy.test" + VIRTUAL_PATH: "/bar/even/deeper" + +web4: + image: web + expose: + - "84" + environment: + WEB_PORTS: "84" + VIRTUAL_HOST: "nginx-proxy.test" + VIRTUAL_PATH: "/bar/even/deeper/with/end/" + + +sut: + image: jwilder/nginx-proxy:test + volumes: + - /var/run/docker.sock:/tmp/docker.sock:ro + - ./lib/ssl/dhparam.pem:/etc/nginx/dhparam/dhparam.pem:ro