Dark avenger with shield blocking evil electrical beams of death

Filtering egress paths

Learn how to filter egress paths using nginx as a reverse proxy to block specific URL paths while allowing access to other parts of a service.

November 15, 2024· 3 min read
0 score

Ah, I see! If service.com is a public service that you want to access from your system, but you need to block certain URL paths on egress (outgoing traffic), the goal is to prevent requests to specific URL paths (like /nasty/analytics) while still allowing access to other parts of service.com.

In this case, modifying /etc/hosts is not relevant because the service is external and publicly available. What you need is a method to block certain URL paths at the network level before your system sends requests to the external service.

Here’s how you can achieve that using nginx as a reverse proxy:

1. Use nginx as a Reverse Proxy for Outbound Traffic

To block specific URL paths on egress (outgoing traffic), we can configure nginx to intercept requests and prevent certain URL paths from being accessed.

Steps to Set Up nginx as a Reverse Proxy for Outgoing Traffic:

  1. Install nginx on your system if it’s not already installed.

    On Ubuntu/Debian-based systems:

    sudo apt updatesudo apt install nginx
  2. Configure nginx to Block Specific URL Paths

    Since you want to block access to specific paths on service.com, you will set up nginx to act as a reverse proxy. It will forward requests to service.com, but block access to specific paths (e.g., /nasty/analytics).

    Create a configuration file for nginx:

    sudo nano /etc/nginx/sites-available/service_proxy

    Add the following configuration:

    server {    listen 8080;  # Listen on a non-standard port for local requests     server_name service.com;     # Block access to specific URL paths (e.g., /nasty/analytics)    location /nasty/analytics {        deny all;        return 403;  # Forbidden response    }     # Forward other requests to the actual service.com    location / {        proxy_pass http://service.com;  # Forward all other traffic to service.com        proxy_set_header Host $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_set_header X-Forwarded-Proto $scheme;    }}

    Explanation:

    • server_name service.com; — This is the domain you're trying to block certain paths from.
    • location /nasty/analytics { deny all; } — This block specifically denies access to /nasty/analytics.
    • location / { proxy_pass http://service.com; } — This forwards all other requests to service.com.
  3. Enable the Configuration

    Create a symlink to enable the site configuration:

    sudo ln -s /etc/nginx/sites-available/service_proxy /etc/nginx/sites-enabled/
  4. Test the Configuration

    Check for syntax errors in the nginx configuration:

    sudo nginx -t

    If the test is successful, you’ll see syntax is okay and test is successful.

  5. Restart nginx

    Restart nginx to apply the new configuration:

    sudo systemctl restart nginx
  6. Configure Your System to Use nginx as a Proxy

    Now you need to configure your system to route traffic through nginx. This way, outgoing requests to service.com will go through nginx, which will apply the URL path block.

    • For a system-wide proxy (e.g., on Linux):
      You can set the system to use nginx as an HTTP proxy by configuring your system’s proxy settings to point to localhost:8080 (or whatever port you chose for nginx).

      Example (using environment variables):

      export http_proxy="http://localhost:8080"export https_proxy="http://localhost:8080"
    • For a browser (e.g., Firefox or Chrome):
      Set the proxy settings in the browser to localhost:8080.

      For Firefox:

      • Go to Preferences > General > Network Settings.
      • Select Manual proxy configuration and set HTTP Proxy to localhost and Port to 8080.

      For Chrome:

      • Use the --proxy-server flag to configure it (e.g., --proxy-server=http://localhost:8080).
  7. Test the Blocking Behavior

    Now, when you try to access http://service.com/nasty/analytics, nginx will return a 403 Forbidden response, blocking the path. All other traffic to service.com will be proxied as usual.


2. DNS or Firewall-Based Solutions (Alternative Methods)

If you don't want to use a proxy, other options involve DNS filtering or firewall rules. However, these approaches are more limited:

  • DNS filtering with tools like dnsmasq could block entire domains but not specific URL paths.
  • Firewall rules (via iptables or nftables) work with IP addresses, not URL paths, so they wouldn't be as effective for this task.

Conclusion:

Using nginx as a reverse proxy allows you to intercept requests to service.com and block specific URL paths like /nasty/analytics. This is the most straightforward and flexible method to block URL paths on egress while allowing other traffic to service.com. Make sure to route your system’s traffic through nginx to enable this filtering.

Dark avenger with shield blocking evil electrical beams of death so they don't get to the people.

Related Articles