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
Release: 2024-02-08 21:12:16
forward
1244 people have browsed it

如何为本地 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;
  }
}
Copy after login

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;
  ...
Copy after login

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
...
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template