Welcome! Log In Create A New Profile

Advanced

Configure Nginx to handle HTTP&HTTPS requests behind GCP Load-balancer

Posted by sdffsdfsdfsdf 
Configure Nginx to handle HTTP&HTTPS requests behind GCP Load-balancer
May 02, 2024 09:11AM
I have a Django app hosted on a GCP instance that has an external IP, the Django is running using Gunicorn on port 8000, when accessing the Django using EXTERNAL_IP:8000 the site works perfectly, but when trying to access the Django using EXTERNAL_IP:18000 the site doesn't work(This site can’t be reached), how to fix the Nginx configuration?

the Django app is hosted on GCP in an unmanaged instance group and connected to GCP load-balancer and all my requests after the LB is HTTP, and I'm using Certificate Manager from GCP, I've tried to make it work but with no luck.

My ultimate goal is to have Nginx configuration like below that will serve HTTP & HTTPS without the need to add SSL certificate at the NGINX level and stay using my website using HTTPS relying on GCP-CertificateManager at LB level.

How my configuration should look like to accomplish this?

This the configuration I trying to use with my Django app.
##############################
server {
server_name _;
listen 18000;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

location / {
try_files $uri u/proxy_to_app;
}

location u/proxy_to_app {
#proxy_set_header X-Forwarded-Port $http_x_forwarded_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_redirect off;
proxy_pass http://127.0.0.1:8000;
}
}
##############################
There is a service I have that uses the same concept I'm trying to accomplish above, but I'm unable to make it work for my Django app.

Working service config(different host):
##############################
upstream pppp_app_server {
server 127.0.0.1:8800 fail_timeout=0;
}

map $http_origin $cors_origin {
default "null";
}

server {
server_name ppp.eeee.com;
listen 18800 ;

if ($host ~ "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") {
set $test_ip_disclosure A;
}

if ($http_x_forwarded_for != "") {
set $test_ip_disclosure "${test_ip_disclosure}B";
}

if ($test_ip_disclosure = AB) {
return 403;
}
if ($http_x_forwarded_proto = "http")
{
set $do_redirect_to_https "true";
}

if ($do_redirect_to_https = "true")
{
return 301 https://$host$request_uri;
}

location ~ ^/static/(?P<file>.*) {
root /xxx/var/ppppp;
add_header 'Access-Control-Allow-Origin' $cors_origin;
add_header 'Vary' 'Accept-Encoding,Origin';

try_files /staticfiles/$file =404;
}

location ~ ^/media/(?P<file>.*) {
root /xxx/var/ppppp;
try_files /media/$file =404;
}

location / {
try_files $uri u/proxy_to_app;
client_max_body_size 4M;
}

location ~ ^/(api)/ {
try_files $uri u/proxy_to_app;
client_max_body_size 4M;
}

location /robots.txt {
root /xxx/app/nginx;
try_files $uri /robots.txt =404;
}

location u/proxy_to_app {
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header X-Forwarded-Port $http_x_forwarded_port;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;

# newrelic-specific header records the time when nginx handles a request.
proxy_set_header X-Queue-Start "t=${msec}";

proxy_set_header Host $http_host;

proxy_redirect off;
proxy_pass http://pppp_app_server;
}
client_max_body_size 4M;
}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 181
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready