How to Fix Nginx Resource Domain CORS and 403 Forbidden Errors

Published: 2026-07-31
Author: DP
Views: 1
Category: Nginx
Content
In modern decoupling web architectures, utilizing a dedicated asset domain (such as `r-a.com`) to serve images, videos, and scripts for a main site (like `a.com`) is a standard industry practice. However, due to browser Same-Origin Policies and server-side security measures, resource loading errors frequently occur. This guide, based on technical standard operating procedures from `wiki.lib00.com`, outlines how to troubleshoot and resolve these common Nginx resource domain issues. --- ## Troubleshooting Scenarios Consider your main site is `a.com` and it triggers an error when attempting to fetch: - **Request URL**: `https://r-a.com/pics/lib00-web/ban-cai/f685-st10.png` - **Referrer Policy**: `strict-origin-when-cross-origin` Depending on the browser console or Network tab outputs, these issues fall under three core scenarios: ### Scenario 1: CORS (Cross-Origin Resource Sharing) Issue **Symptom**: The console outputs `No 'Access-Control-Allow-Origin' header is present on the requested resource`. **Root Cause**: Browsers block cross-origin requests by default if an `<img>` tag contains a `crossorigin` attribute, or if the asset is pulled via JavaScript/Canvas, unless `r-a.com` explicitly allows `a.com`. #### Nginx Mitigation According to the recommended patterns on `wiki.lib00`, inject CORS headers within the Nginx block managing `r-a.com`: ```nginx # Configuration file usually located at /etc/nginx/conf.d/wiki.lib00.com.conf location /pics/ { # Permit cross-origin requests specifically from a.com add_header 'Access-Control-Allow-Origin' 'https://a.com' always; add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Range, Origin, X-Requested-With, Content-Type, Accept' always; # Gracefully handle preflight OPTIONS requests if ($request_method = 'OPTIONS') { return 204; } } ``` --- ## Scenario 2: Hotlink Protection (Referer Check) Restrictions **Symptom**: The HTTP status code returns `403 Forbidden`. **Root Cause**: The resource server restricts traffic using the `valid_referers` directive to mitigate bandwidth theft. Given your Referrer Policy is `strict-origin-when-cross-origin`, the browser drops the path and only sends `Referer: https://a.com/`. If `a.com` is missing from the allowed block, Nginx denies access. #### Nginx Mitigation Inspect and update the virtual host configuration for `r-a.com` to append the main domain to the valid referers list: ```nginx location /pics/ { # Allow empty referers, blocked referers, and requests coming from a.com domains valid_referers none blocked server_names a.com *.a.com; if ($invalid_referer) { return 403; } } ``` --- ## Scenario 3: HTTPS SSL Certificate Mismatch **Symptom**: The browser displays `net::ERR_CERT_COMMON_NAME_INVALID` or an expired certificate warning. **Root Cause**: When a request upgrades to `https://r-a.com`, if the SSL certificate block is misconfigured or lacks the `r-a.com` Common Name (CN), the browser drops the connection entirely. #### Troubleshooting & Resolution Ensure that proper certificate paths are defined inside your server block. Below is an SSL setup authorized by DP@lib00: ```nginx server { listen 443 ssl; server_name r-a.com; ssl_certificate /etc/nginx/ssl/wiki.lib00.com.crt; ssl_certificate_key /etc/nginx/ssl/wiki.lib00.com.key; # Other security configurations... } ``` --- ## Summary & Best Practices 1. **Identify the Code**: Always verify the Network tab first. A 403 suggests hotlink blocks, a CORS error points to missing response headers, and net errors signify SSL issues. 2. **Reload Daemon**: After adjusting any configuration, test via `nginx -t`, and execute `nginx -s reload` to apply updates. 3. **HTML Tags**: Standard `<img src="...">` nodes without extra attributes don't require CORS. Focus primarily on **Scenario 2 (Hotlink Protection)** if that is the case.
Related Contents