我如何配置django rest框架分页URL

时间:2015-08-23 12:17:34

标签: django django-rest-framework

当我在django rest框架中获得一个对象时,url总是与localhost绝对,但是在生产中我通过nginx上的代理,有没有办法在设置中设置这个url

实施例

count: 11
next: "http://localhost:8000/api/accounts/?ordering=-date_registered&page=2"
previous: null

我需要它

count: 11
next: "http:/example.com/api/accounts/?ordering=-date_registered&page=2"
previous: null

----------编辑--------------------------

请参阅我的完整nginx配置

server {
    listen 80;
    server_name 123.123.123.123;
    root            /home/admin/www/site-web/dist;
    index           index.html;
    charset         utf-8;


    location /static/ {
        alias /home/admin/www/site/static/;
    }

    location /media/  {
        alias /home/admin/www/site/media/;
    }

    location /nginx_status/ {
        # Turn on nginx stats
        stub_status on;
        # I do not need logs for stats
        access_log  off;
        # Security: Only allow access from 192.168.1.100 IP #
        # allow 192.168.1.100;
        # Send rest of the world to /dev/null #
        # deny all;
   }

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        try_files $uri $uri/ /index.html;

       if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            #
            # Om nom nom cookies
            #
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
             }

        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }

        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         }
    }

   location /docs/ {
       proxy_pass http://127.0.0.1:8000/docs/;
       break;
   }

   location /api/ {
       underscores_in_headers on;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $http_host;
       proxy_redirect off;
       proxy_pass http://127.0.0.1:8000/api/;
       break;
   }

   location /admin/ {
       proxy_pass http://127.0.0.1:8000/admin/;
       break;
   }

}

====超级编辑====

对不起,伙计们,我已经强调了这些内容;'我删除了它,一切正常

=====

1 个答案:

答案 0 :(得分:8)

听起来你的Host标题没有正确设置,这在你的nginx配置中会出现问题。问题是您发送的Host标头包含端口号,因此Django在构建网址时包含端口号。这将导致CSRF未来出现问题,因为CSRF检查会在您不进行调试时执行严格的端口检查。

This is known to cause issues with SSL for similar reasons.

您可以通过将Nginx中的Host标头设置为不包含代理端口来解决此问题。

proxy_set_header Host $http_host;

请注意,我使用的是$http_host变量,而不是$host$host:$server_port。这将确保Django仍然会在非标准端口上尊重CSRF请求,同时仍然为您提供正确的绝对URL。