Skip to content

Commit f1c54a2

Browse files
committed
Configure NGINX using API_ROOT & CONTENT_PATH_PREFIX
fixes: #605
1 parent 5175d61 commit f1c54a2

File tree

6 files changed

+64
-102
lines changed

6 files changed

+64
-102
lines changed

CHANGES/605.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NGINX is now configured to use the values of API_ROOT and CONTENT_PATH_PREFIX

images/pulp_ci_centos/Containerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ RUN dnf -y install postgresql && \
3333
COPY images/s6_assets/openssl.cnf /etc/ssl/pulp/openssl.cnf
3434
COPY images/s6_assets/v3.cnf /etc/ssl/pulp/v3.cnf
3535
COPY images/s6_assets/wait_on_database_migrations.sh /database/assets/wait_on_database_migrations.sh
36-
COPY images/s6_assets/ssl_nginx.conf /nginx/ssl_nginx.conf
37-
COPY images/s6_assets/nginx.conf /nginx/nginx.conf
36+
COPY images/s6_assets/template_nginx.py /nginx/template_nginx.py
37+
COPY images/s6_assets/nginx.conf.j2 /nginx/nginx.conf.j2
3838
COPY images/s6_assets/s6-rc.d /etc/s6-overlay/s6-rc.d
3939
COPY images/s6_assets/init /etc/init
4040
COPY images/s6_assets/fix-attrs.d /etc/fix-attrs.d

images/s6_assets/init/nginx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#!/bin/bash
22

3-
if [ "${PULP_HTTPS,,}" = "true" ]; then
4-
cp -avr /nginx/ssl_nginx.conf /etc/nginx/nginx.conf
5-
else
6-
cp -avr /nginx/nginx.conf /etc/nginx/nginx.conf
7-
fi
3+
python3 /nginx/template_nginx.py /nginx/nginx.conf.j2 /etc/nginx/nginx.conf
84

95
exec /usr/sbin/nginx

images/s6_assets/nginx.conf

Lines changed: 0 additions & 89 deletions
This file was deleted.

images/s6_assets/ssl_nginx.conf renamed to images/s6_assets/nginx.conf.j2

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ http {
3131

3232
server {
3333
# Gunicorn docs suggest the use of the "deferred" directive on Linux.
34+
{% if https | default(false) -%}
3435
listen 443 default_server deferred ssl;
3536

3637
ssl_certificate /etc/pulp/certs/pulp_webserver.crt;
@@ -46,7 +47,9 @@ http {
4647

4748
# HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
4849
add_header Strict-Transport-Security max-age=15768000;
49-
50+
{%- else -%}
51+
listen 80 default_server deferred;
52+
{%- endif %}
5053
server_name $hostname;
5154

5255
# The default client_max_body_size is 1m. Clients uploading
@@ -56,7 +59,7 @@ http {
5659
# Gunicorn docs suggest this value.
5760
keepalive_timeout 5;
5861

59-
location /pulp/content/ {
62+
location {{ content_path }} {
6063
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6164
proxy_set_header X-Forwarded-Proto $scheme;
6265
proxy_set_header Host $http_host;
@@ -66,7 +69,19 @@ http {
6669
proxy_pass http://pulp-content;
6770
}
6871

69-
location /pulp/api/v3/ {
72+
location {{ api_root }}api/v3/ {
73+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
74+
proxy_set_header X-Forwarded-Proto $scheme;
75+
proxy_set_header Host $http_host;
76+
# we don't want nginx trying to do something clever with
77+
# redirects, we set the Host: header above already.
78+
proxy_redirect off;
79+
proxy_pass http://pulp-api;
80+
client_max_body_size 0;
81+
}
82+
83+
{%- if domain_enabled | default(false) %}
84+
location ~ {{ api_root }}.+/api/v3/ {
7085
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7186
proxy_set_header X-Forwarded-Proto $scheme;
7287
proxy_set_header Host $http_host;
@@ -76,6 +91,7 @@ http {
7691
proxy_pass http://pulp-api;
7792
client_max_body_size 0;
7893
}
94+
{%- endif %}
7995

8096
location /auth/login/ {
8197
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@@ -101,17 +117,18 @@ http {
101117
# http://whitenoise.evans.io/en/stable/
102118
}
103119

120+
{%- if https | default(false) %}
104121
# ACME http-01 tokens, i.e, for Let's Encrypt
105122
location /.well-known/ {
106123
try_files $uri $uri/ =404;
107124
}
108-
125+
{%- endif %}
109126
}
110-
127+
{%- if https | default(false) %}
111128
server {
112129
listen 80 default_server;
113130
server_name _;
114131
return 301 https://$host$request_uri;
115132
}
116-
133+
{%- endif %}
117134
}

images/s6_assets/template_nginx.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import argparse
2+
import os
3+
import django
4+
from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
5+
6+
from jinja2 import Template
7+
8+
9+
if __name__ == "__main__":
10+
parser = argparse.ArgumentParser(
11+
description="Create Pulp's nginx conf file based on current settings.",
12+
)
13+
parser.add_argument("template_file", type=open)
14+
parser.add_argument("output_file", type=argparse.FileType("w"))
15+
args = parser.parse_args()
16+
17+
https = os.getenv("PULP_HTTPS", "false")
18+
values = {
19+
"https": https.lower() == "true",
20+
"api_root": "/pulp/",
21+
"content_path": "/pulp/content/",
22+
"domain_enabled": False,
23+
}
24+
25+
try:
26+
django.setup()
27+
from django.conf import settings
28+
except (AppRegistryNotReady, ImproperlyConfigured):
29+
print("Failed to find settings for nginx template, using defaults")
30+
else:
31+
values["api_root"] = settings.API_ROOT
32+
values["content_path"] = settings.CONTENT_PATH_PREFIX
33+
values["domain_enabled"] = settings.DOMAIN_ENABLED
34+
35+
template = Template(args.template_file.read())
36+
output = template.render(**values)
37+
args.output_file.write(output)

0 commit comments

Comments
 (0)