Commit bd3cc376 by Arbab Nazar

Merge pull request #2594 from edx/arbab/ops-1208

OPS-1208 Refactor Nginx SSL redirect bahavior
parents 2bff4f65 57ee6985
...@@ -11,6 +11,7 @@ NGINX_EDXAPP_EXTRA_CONFIGS: [] ...@@ -11,6 +11,7 @@ NGINX_EDXAPP_EXTRA_CONFIGS: []
NGINX_EDXAPP_CUSTOM_REDIRECTS: {} NGINX_EDXAPP_CUSTOM_REDIRECTS: {}
NGINX_ENABLE_SSL: False NGINX_ENABLE_SSL: False
NGINX_REDIRECT_TO_HTTPS: False
# Set these to real paths on your # Set these to real paths on your
# filesystem, otherwise nginx will # filesystem, otherwise nginx will
# use a self-signed snake-oil cert # use a self-signed snake-oil cert
......
...@@ -30,20 +30,38 @@ server { ...@@ -30,20 +30,38 @@ server {
error_page {{ k }} {{ v }}; error_page {{ k }} {{ v }};
{% endfor %} {% endfor %}
listen {{ EDXAPP_CMS_NGINX_PORT }} {{ default_site }};
{% if NGINX_ENABLE_SSL %} {% if NGINX_ENABLE_SSL %}
listen {{ EDXAPP_CMS_NGINX_PORT }} {{ default_site }};
listen {{ EDXAPP_CMS_SSL_NGINX_PORT }} ssl; listen {{ EDXAPP_CMS_SSL_NGINX_PORT }} ssl;
ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }}; ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }};
ssl_certificate_key /etc/ssl/private/{{ NGINX_SSL_KEY|basename }}; ssl_certificate_key /etc/ssl/private/{{ NGINX_SSL_KEY|basename }};
# request the browser to use SSL for all connections # request the browser to use SSL for all connections
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
{% else %}
listen {{ EDXAPP_CMS_NGINX_PORT }} {{ default_site }};
{% endif %} {% endif %}
{% if NGINX_REDIRECT_TO_HTTPS %}
# Redirect http to https over single instance
if ($scheme != "https")
{
set $do_redirect_to_https "true";
}
# Nginx does not support nested conditions
# Forward to HTTPS if we're an HTTP request... and the server is behind ELB
if ($http_x_forwarded_proto = "http")
{
set $do_redirect_to_https "true";
}
if ($do_redirect_to_https = "true")
{
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %}
server_name {{ CMS_HOSTNAME }}; server_name {{ CMS_HOSTNAME }};
access_log {{ nginx_log_dir }}/access.log {{ NGINX_LOG_FORMAT_NAME }}; access_log {{ nginx_log_dir }}/access.log {{ NGINX_LOG_FORMAT_NAME }};
...@@ -91,19 +109,4 @@ error_page {{ k }} {{ v }}; ...@@ -91,19 +109,4 @@ error_page {{ k }} {{ v }};
{% include "robots.j2" %} {% include "robots.j2" %}
{% include "static-files.j2" %} {% include "static-files.j2" %}
{% if NGINX_SET_X_FORWARDED_HEADERS %}
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
{% else %}
# Forward to HTTPS if we're an HTTP request...
if ($http_x_forwarded_proto = "http") {
set $do_redirect "true";
}
# Run our actual redirect...
if ($do_redirect = "true") {
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %}
} }
...@@ -18,18 +18,36 @@ upstream ecommerce_app_server { ...@@ -18,18 +18,36 @@ upstream ecommerce_app_server {
server { server {
server_name {{ ECOMMERCE_HOSTNAME }}; server_name {{ ECOMMERCE_HOSTNAME }};
listen {{ ECOMMERCE_NGINX_PORT }} {{ default_site }};
{% if NGINX_ENABLE_SSL %} {% if NGINX_ENABLE_SSL %}
listen {{ ECOMMERCE_NGINX_PORT }} {{ default_site }};
listen {{ ECOMMERCE_SSL_NGINX_PORT }} ssl; listen {{ ECOMMERCE_SSL_NGINX_PORT }} ssl;
ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }}; ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }};
ssl_certificate_key /etc/ssl/private/{{ NGINX_SSL_KEY|basename }}; ssl_certificate_key /etc/ssl/private/{{ NGINX_SSL_KEY|basename }};
# request the browser to use SSL for all connections # request the browser to use SSL for all connections
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
{% endif %}
{% else %} {% if NGINX_REDIRECT_TO_HTTPS %}
listen {{ ECOMMERCE_NGINX_PORT }} {{ default_site }}; # Redirect http to https over single instance
if ($scheme != "https")
{
set $do_redirect_to_https "true";
}
# Nginx does not support nested conditions
# Forward to HTTPS if we're an HTTP request... and the server is behind ELB
if ($http_x_forwarded_proto = "http")
{
set $do_redirect_to_https "true";
}
if ($do_redirect_to_https = "true")
{
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %} {% endif %}
location ~ ^/static/(?P<file>.*) { location ~ ^/static/(?P<file>.*) {
...@@ -53,20 +71,5 @@ location @proxy_to_app { ...@@ -53,20 +71,5 @@ location @proxy_to_app {
proxy_pass http://ecommerce_app_server; proxy_pass http://ecommerce_app_server;
} }
{% if NGINX_SET_X_FORWARDED_HEADERS %}
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
{% else %}
# Forward to HTTPS if we're an HTTP request...
if ($http_x_forwarded_proto = "http") {
set $do_redirect "true";
}
# Run our actual redirect...
if ($do_redirect = "true") {
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %}
} }
...@@ -33,19 +33,23 @@ location @proxy_to_app { ...@@ -33,19 +33,23 @@ location @proxy_to_app {
proxy_pass http://insights_app_server; proxy_pass http://insights_app_server;
} }
{% if NGINX_SET_X_FORWARDED_HEADERS %} {% if NGINX_REDIRECT_TO_HTTPS %}
if ($scheme != "https") { # Redirect http to https over single instance
rewrite ^ https://$host$uri permanent; if ($scheme != "https")
} {
{% else %} set $do_redirect_to_https "true";
# Forward to HTTPS if we're an HTTP request...
if ($http_x_forwarded_proto = "http") {
set $do_redirect "true";
} }
# Run our actual redirect... # Nginx does not support nested conditions
if ($do_redirect = "true") { # Forward to HTTPS if we're an HTTP request... and the server is behind ELB
rewrite ^ https://$host$request_uri? permanent; if ($http_x_forwarded_proto = "http")
{
set $do_redirect_to_https "true";
} }
{% endif %}
if ($do_redirect_to_https = "true")
{
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %}
} }
...@@ -76,19 +76,23 @@ server { ...@@ -76,19 +76,23 @@ server {
expires epoch; expires epoch;
} }
{% if NGINX_SET_X_FORWARDED_HEADERS %} {% if NGINX_REDIRECT_TO_HTTPS %}
if ($scheme != "https") { # Redirect http to https over single instance
rewrite ^ https://$host$uri permanent; if ($scheme != "https")
} {
{% else %} set $do_redirect_to_https "true";
# Forward to HTTPS if we're an HTTP request...
if ($http_x_forwarded_proto = "http") {
set $do_redirect "true";
} }
# Run our actual redirect... # Nginx does not support nested conditions
if ($do_redirect = "true") { # Forward to HTTPS if we're an HTTP request... and the server is behind ELB
rewrite ^ https://$host$request_uri? permanent; if ($http_x_forwarded_proto = "http")
{
set $do_redirect_to_https "true";
} }
{% endif %}
if ($do_redirect_to_https = "true")
{
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %}
} }
...@@ -51,21 +51,37 @@ server { ...@@ -51,21 +51,37 @@ server {
error_page {{ k }} {{ v }}; error_page {{ k }} {{ v }};
{% endfor %} {% endfor %}
listen {{ EDXAPP_LMS_NGINX_PORT }} {{ default_site }};
{% if NGINX_ENABLE_SSL %} {% if NGINX_ENABLE_SSL %}
listen {{ EDXAPP_LMS_NGINX_PORT }} {{ default_site }};
listen {{ EDXAPP_LMS_SSL_NGINX_PORT }} {{ default_site }} ssl; listen {{ EDXAPP_LMS_SSL_NGINX_PORT }} {{ default_site }} ssl;
ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }}; ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }};
ssl_certificate_key /etc/ssl/private/{{ NGINX_SSL_KEY|basename }}; ssl_certificate_key /etc/ssl/private/{{ NGINX_SSL_KEY|basename }};
# request the browser to use SSL for all connections # request the browser to use SSL for all connections
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
{% else %}
listen {{ EDXAPP_LMS_NGINX_PORT }} {{ default_site }};
{% endif %} {% endif %}
{% if NGINX_REDIRECT_TO_HTTPS %}
# Redirect http to https over single instance
if ($scheme != "https")
{
set $do_redirect_to_https "true";
}
# Nginx does not support nested conditions
# Forward to HTTPS if we're an HTTP request... and the server is behind ELB
if ($http_x_forwarded_proto = "http")
{
set $do_redirect_to_https "true";
}
if ($do_redirect_to_https = "true")
{
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %}
access_log {{ nginx_log_dir }}/access.log {{ NGINX_LOG_FORMAT_NAME }}; access_log {{ nginx_log_dir }}/access.log {{ NGINX_LOG_FORMAT_NAME }};
error_log {{ nginx_log_dir }}/error.log error; error_log {{ nginx_log_dir }}/error.log error;
...@@ -185,19 +201,4 @@ location ~ ^{{ EDXAPP_MEDIA_URL }}/(?P<file>.*) { ...@@ -185,19 +201,4 @@ location ~ ^{{ EDXAPP_MEDIA_URL }}/(?P<file>.*) {
{% include "robots.j2" %} {% include "robots.j2" %}
{% include "static-files.j2" %} {% include "static-files.j2" %}
{% if NGINX_SET_X_FORWARDED_HEADERS %}
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
{% else %}
# Forward to HTTPS if we're an HTTP request...
if ($http_x_forwarded_proto = "http") {
set $do_redirect "true";
}
# Run our actual redirect...
if ($do_redirect = "true") {
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %}
} }
...@@ -17,10 +17,11 @@ upstream programs_app_server { ...@@ -17,10 +17,11 @@ upstream programs_app_server {
server { server {
server_name {{ PROGRAMS_HOSTNAME }}; server_name {{ PROGRAMS_HOSTNAME }};
listen {{ PROGRAMS_NGINX_PORT }} {{ default_site }};
{% if NGINX_ENABLE_SSL %} {% if NGINX_ENABLE_SSL %}
listen {{ PROGRAMS_NGINX_PORT }} {{ default_site }};
listen {{ PROGRAMS_SSL_NGINX_PORT }} ssl; listen {{ PROGRAMS_SSL_NGINX_PORT }} ssl;
ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }}; ssl_certificate /etc/ssl/certs/{{ NGINX_SSL_CERTIFICATE|basename }};
...@@ -28,8 +29,26 @@ server { ...@@ -28,8 +29,26 @@ server {
# Request that the browser use SSL for all connections. # Request that the browser use SSL for all connections.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
{% else %} {% endif %}
listen {{ PROGRAMS_NGINX_PORT }} {{ default_site }};
{% if NGINX_REDIRECT_TO_HTTPS %}
# Redirect http to https over single instance
if ($scheme != "https")
{
set $do_redirect_to_https "true";
}
# Nginx does not support nested conditions
# Forward to HTTPS if we're an HTTP request... and the server is behind ELB
if ($http_x_forwarded_proto = "http")
{
set $do_redirect_to_https "true";
}
if ($do_redirect_to_https = "true")
{
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %} {% endif %}
location ~ ^/static/(?P<file>.*) { location ~ ^/static/(?P<file>.*) {
...@@ -61,19 +80,5 @@ location @proxy_to_app { ...@@ -61,19 +80,5 @@ location @proxy_to_app {
proxy_redirect off; proxy_redirect off;
proxy_pass http://programs_app_server; proxy_pass http://programs_app_server;
} }
{% if NGINX_SET_X_FORWARDED_HEADERS %}
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
{% else %}
# Forward to HTTPS if we're an HTTP request...
if ($http_x_forwarded_proto = "http") {
set $do_redirect "true";
}
# Run our actual redirect...
if ($do_redirect = "true") {
rewrite ^ https://$host$request_uri? permanent;
}
{% endif %}
} }
...@@ -174,6 +174,7 @@ COURSE_DISCOVERY_SSL_NGINX_PORT: 443 ...@@ -174,6 +174,7 @@ COURSE_DISCOVERY_SSL_NGINX_PORT: 443
COURSE_DISCOVERY_VERSION: $course_discovery_version COURSE_DISCOVERY_VERSION: $course_discovery_version
NGINX_SET_X_FORWARDED_HEADERS: True NGINX_SET_X_FORWARDED_HEADERS: True
NGINX_REDIRECT_TO_HTTPS: True
EDX_ANSIBLE_DUMP_VARS: true EDX_ANSIBLE_DUMP_VARS: true
migrate_db: "yes" migrate_db: "yes"
openid_workaround: True openid_workaround: True
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment