문제

그래서 정적 미디어 용 Nginx 및로드 밸런싱 및 토네이도를 Django (4 개의 서버 실행)로 Webserver로 간단한 설정을 받았습니다. 내 문제는 remote_addr이 django로 넘어 가지 않기 때문에 키 오버를 얻는 것입니다.

article.ip = request.META['REMOTE_ADDR']

원격 주소는 nginx.conf 덕분에 Xeal-IP (http_x_real_ip)로 전송됩니다.

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect false;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://frontends;
    }

http가 메타 키로 선정되므로 proxy_set_header remote_addr $ remote_addr를 할 수는 없습니다. 내가 할 수있는 일은 원격 Addr 키를 찾지 못하면 Xeal-IP를 읽는 것입니다. 그러나 더 똑똑한 솔루션이 있는지 궁금합니다.

감사!

도움이 되었습니까?

해결책

이거 한번 해봐:

location / {
    proxy_pass http://frontends;
    proxy_pass_header Server;
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header REMOTE_ADDR $remote_addr;
}

그냥 추가하십시오 proxy_set_header REMOTE_ADDR 그리고 그것은 잘 작동해야합니다.

시도 :

  • 장고 1.5.4
  • nginx 1.4.3
  • 토네이도 2.2.1

다른 팁

다음은 문제를 해결하는 방법입니다. 이 미들웨어를 사용하여 :

class SetRemoteAddrMiddleware(object):
    def process_request(self, request):
        if not request.META.has_key('REMOTE_ADDR'):
            try:
                request.META['REMOTE_ADDR'] = request.META['HTTP_X_REAL_IP']
            except:
                request.META['REMOTE_ADDR'] = '1.1.1.1' # This will place a valid IP in REMOTE_ADDR but this shouldn't happen

도움이되기를 바랍니다!

비슷한 설정이 있습니다. Apache 앞에 Nginx를 넣은 후 Apache 로그의 IP가 항상 127.0.0.1임을 알았습니다. "libapache2-mod-rpaf"를 설치하는 것은 그것을 고치는 것처럼 보였다. 당신의 문제가 관련되어 있는지 전혀 모릅니다.

"fastcgi_param remote_addr $ remote_addr;" nginx.conf 파일로 :

    location / {
    # host and port to fastcgi server
    fastcgi_pass 127.0.0.1:8801;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_pass_header Authorization;
    fastcgi_intercept_errors off;
    ...
    # Add this line!
    fastcgi_param REMOTE_ADDR $remote_addr;
    ...
}

원천: Django 용 가상 서버 + FCGI를 Nginx하는 방법은 무엇입니까?

저에게 다음과 같은 작업을 사용했습니다.

server {
    listen 80;
    server_name foo.bar.com;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

이것은 Django 1.4 (특히 LocalShop)에서 작동합니다.

아니요 불가능합니다 remote_addr을 통과합니다. 따라서 내가 아는 유일한 솔루션은 Xeal-IP 또는 X-Forwarded-For를 사용하고 백엔드가 올바르게 처리하는지 확인하는 것입니다.

편집 : 이것은 일반적인 nginx proxy_pass가 아닌 fastcgi_pass에 적용됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top