基于Nginx的自定义外链重定向与点击追踪方案咨询
Great question! You’re right that Bitly and Google Analytics don’t fit your needs perfectly—Bitly restricts multiple short links pointing to the same target, and GA isn’t built for tracking redirects to external sites where you can’t deploy code. Let’s break down three tailored solutions that give you full control over both the redirects and click tracking:
1. Nginx + Custom Logging + Simple Script (Lightweight, Self-Managed)
If you want a no-frills, cost-free approach without third-party services, this is ideal. You’ll configure Nginx to log unique identifiers (like a or b from /twitter/a) alongside click details, then use a simple script to tally clicks.
Step 1: Nginx Configuration
Update your Nginx server block to capture the identifier, log it, and redirect to LinkedIn:
server { listen 80; server_name www.domain.com; # Match /twitter/[identifier] paths location ~ ^/twitter/([a-zA-Z0-9_-]+)$ { # Custom log to track identifiers and click metadata access_log /var/log/nginx/twitter_redirects.log custom_twitter_format; # 302 redirect to your LinkedIn profile return 302 https://www.linkedin.com/in/your-profile/; } # Define the custom log format to include the captured identifier log_format custom_twitter_format '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "identifier:$1"'; }
Step 2: Analyze Logs with a Script
Write a quick Python or shell script to parse the log file and count clicks per identifier. Example Python script:
from collections import defaultdict import re log_file = "/var/log/nginx/twitter_redirects.log" identifier_pattern = r'identifier:(\w+)' click_counts = defaultdict(int) with open(log_file, "r") as f: for line in f: match = re.search(identifier_pattern, line) if match: click_counts[match.group(1)] += 1 # Print results for id, count in click_counts.items(): print(f"Identifier '{id}': {count} total clicks")
Pros: Full control, no external dependencies, zero cost.
Cons: No built-in visualization—you’ll need to run the script manually or set up a cron job to generate reports.
2. Nginx + Self-Hosted Short Link Tool (YOURLS)
If you want a user-friendly interface for tracking clicks and managing your redirects, YOURLS (Your Own URL Shortener) is perfect. It’s open-source, self-hosted, and lets you create multiple custom short codes (like a and b) that all point to the same LinkedIn URL.
Step 1: Deploy YOURLS
Set up YOURLS on your server (requires PHP and MySQL). Follow the official installation guide to get it running.
Step 2: Nginx Integration
Configure Nginx to forward /twitter/[identifier] requests to YOURLS:
location ~ ^/twitter/([a-zA-Z0-9_-]+)$ { proxy_pass http://localhost/yourls/yourls-api.php?keyword=$1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
Step 3: Create Custom Short Codes
In the YOURLS admin dashboard, create short codes a and b—both mapped to your LinkedIn profile. YOURLS will automatically track clicks for each code, with built-in stats like click timestamps, user locations, and device types.
Pros: Visual dashboard, advanced tracking, easy to manage multiple identifiers.
Cons: Requires maintaining a PHP/MySQL stack, but it’s lightweight and well-documented.
3. Nginx + Privacy-First Analytics (Plausible/Matomo) + Middle Redirect Page
If you want detailed user analytics (like where clicks are coming from, device types, etc.) without relying on Google Analytics, use a self-hosted analytics tool like Plausible or Matomo with a temporary middle page to track the redirect.
Step 1: Deploy Plausible/Matomo
Set up your preferred analytics tool on your server (Plausible is simpler, Matomo offers more features).
Step 2: Create a Tracking Page
Make a simple HTML page (tracker.html) that sends a custom event to your analytics tool, then redirects to LinkedIn:
<!DOCTYPE html> <html> <head> <title>Redirecting...</title> <!-- Plausible tracking code (adjust for your domain) --> <script defer data-domain="www.domain.com" src="/js/plausible.js"></script> <script> // Capture the identifier from the URL parameter const urlParams = new URLSearchParams(window.location.search); const identifier = urlParams.get('id'); // Send a custom event to Plausible plausible('TwitterRedirect', { props: { identifier: identifier } }); // Redirect after a brief delay (so the event is sent) setTimeout(() => { window.location.href = 'https://www.linkedin.com/in/your-profile/'; }, 300); </script> </head> <body> Redirecting to LinkedIn... </body> </html>
Step 3: Nginx Configuration
Update Nginx to redirect /twitter/[identifier] to your tracking page with the identifier as a parameter:
server { listen 80; server_name www.domain.com; location ~ ^/twitter/([a-zA-Z0-9_-]+)$ { return 302 /tracker.html?id=$1; } location /tracker.html { root /var/www/html; expires 1h; # Cache the page for performance } }
Pros: Detailed, privacy-friendly analytics, custom event tracking, no reliance on big tech.
Cons: Adds a tiny delay to the redirect (300ms is barely noticeable), requires setting up an analytics tool.
Which Should You Choose?
- Lightweight, no maintenance: Go with Nginx + custom logging.
- Easy click management with a dashboard: Use YOURLS.
- Advanced user analytics: Pick Plausible/Matomo with a middle page.
内容的提问来源于stack exchange,提问作者RSX

