非反代情况下Django 中 ip = request.META['REMOTE_ADDR'] 即可拿到对应ip地址

Nginx、fcgi、uwsgi等反代情况下

vi /etc/nginx/conf.d/xxx.conf 加入下面几行

location / {

        proxy_pass http://127.0.0.1:8080;

        proxy_redirect off;

     proxy_set_header        Host    $host;

        proxy_set_header        REMOTE_ADDR     $remote_addr;

        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   real_ip_recursive on;

}

完整Nginx配置模板

 1 server {
 2     listen 80;
 3     server_name xxx;
 4     access_log /var/log/nginx/xxx.access_log;
 5     error_log /var/log/nginx/xxx.error_log;
 6   
 7     location /static/  {
 8         alias /data/vhost/xxx/static/;
 9         expires      30d;
10     }
11 
12     location / {
13         proxy_pass http://127.0.0.1:8080;
14     proxy_redirect off;
15         proxy_set_header        Host    $host;
16         proxy_set_header        REMOTE_ADDR     $remote_addr;
17         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
18         real_ip_recursive on;
19     proxy_max_temp_file_size 0;
20         proxy_connect_timeout      90;
21         proxy_send_timeout         90;
22         proxy_read_timeout         90;
23         proxy_buffer_size          4k;
24         proxy_buffers              4 32k;
25         proxy_busy_buffers_size    64k;
26         proxy_temp_file_write_size 64k;
27     }
28 }
xxx.conf

相关文章:

  • 2021-05-25
  • 2021-10-23
  • 2022-12-23
  • 2021-11-16
  • 2021-09-22
  • 2022-01-07
  • 2021-11-28
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-03
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案