Skip to content
The Chat Archive
wizardlm-70b Arena Human Preference 55K

Write me an NGINX location block to proxy_pass based on referer value

  1. User

    Write me an NGINX location block to proxy_pass based on referer value

  2. Assistant

    To create a location block in NGINX that proxy_pass based on the referer value, you can use the following configuration:

    http {
        # ... other server block configurations ...
    
        server {
            listen 80;
            server_name example.com;
    
            # ... other location blocks ...
    
            # Location block to check referer and proxy_pass
            location /proxy {
                proxy_pass http://backend_server_address;
    
                # Set the referer to the client's request
                proxy_set_header Referer $http_referer;
    
                # Add other necessary proxy headers, if needed
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
    
                # Check if the referer is allowed
                if ($http_referer ~* (^|  )allowed_referer_domain.com/(.+))$ {
                    # If allowed, proxy_pass
                    proxy_pass http://backend_server_address;
                } else {
                    # If not allowed, return 403 Forbidden
                    return 403;
                }
            }
        }
    }
    

    This configuration sets up a server block that listens on port 80 for the domain example.com. It includes a location block for the /proxy endpoint, which is responsible for proxying requests based on the referer value.

    Within the /proxy location block, we set the proxy_pass to the backend server's address and add the necessary proxy headers. We then check the $http_referer header value to ensure it matches the allowed domain and path. If the referer is allowed, the proxy request is processed; otherwise, a 403 Forbidden response is returned to the client.

    Make sure to replace backend_server_address and allowed_referer_domain.com with your actual backend server address and the allowed referer domain. You can also modify the regular expression to include additional allowed referer domains or paths.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.