Home >Backend Development >Python Tutorial >How to set up Nginx reverse proxy for local Docker Django

How to set up Nginx reverse proxy for local Docker Django

WBOY
WBOYforward
2024-02-08 21:12:161255browse

如何为本地 Docker Django 设置 Nginx 反向代理

Question content

I am developing a docker project using nginx and django services. I have parameterized django.conf.template to dynamically pass environment variables based on the environment.

django.conf:

upstream django_app {
  server  ${django_private_ip}:${django_port};
}

server {

  listen  80;
  listen  443 ssl;
  listen  [::]:443 ssl;
  server_name   ${nginx_server_name};

  ssl_certificate /etc/nginx/certs/elitecars_cert.pem;
  ssl_certificate_key /etc/nginx/certs/elitecars_privkey.pem;

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

  location / {

    proxy_set_header    x-forwarded-host   $host;
    proxy_set_header    x-forwarded-server $host;
    proxy_set_header    x-forwarded-for    $proxy_add_x_forwarded_for;
    proxy_set_header    x-forwarded-proto  $scheme;
    proxy_set_header    x-real-ip          $remote_addr;
    proxy_set_header    host               $host;

    proxy_redirect  off;
    proxy_pass  http://django_app;
  }
}

The template works fine because I can view the environment variable values ​​using the more /etc/nginx/conf.d/sites-available/django.conf command

upstream django_app {
  server  django:8000;
}

server {

  listen  80;
  listen  443 ssl;
  listen  [::]:443 ssl;
  server_name   0.0.0.0 127.0.0.1 localhost;
  ...

But when I try to access via browser it doesn't work.

Any ideas? Can anyone help me?


Correct answer


On local and mac, I need to add host.docker.internal ip to /etc/hosts of nginx configuration file as shown below :

127.0.0.1        localhost
172.17.0.1       localhost
...

and solve the problem.

The above is the detailed content of How to set up Nginx reverse proxy for local Docker Django. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete