Configure Nginx for WeChat Domain Verification Without Uploading Files
This guide shows how to use Nginx configuration to serve WeChat verification files directly, eliminating the need for manual file uploads and enabling a more secure, automated deployment for business, server, or JS security domains.
Applicable to WeChat public account, mini program, WeChat Pay domain verification, no manual file upload needed, one-line config.
1. Background
When configuring "business domain", "server domain", or "JS security domain" in the WeChat public platform or mini program backend, WeChat requires uploading a verification file named MP_verify_xxxxxx.txt to the website root and ensuring it is publicly accessible via https://yourdomain.com/MP_verify_xxxxxx.txt.
Traditionally the file is manually created and uploaded, but if you use Nginx you can skip this step by configuring Nginx to return the verification content directly.
2. Nginx Configuration Method
Assume the verification file provided by WeChat is:
File name: MP_verify_pydasYvevk9k84q7.txt File content: pydasYvevk9k84q7 Add the following configuration inside the server block:
location ^~ /MP_verify_pydasYvevk9k84q7.txt {
add_header Content-Type "text/plain; charset=utf-8";
default_type text/plain;
return 200 "pydasYvevk9k84q7";
}Configuration explanation:
location ^~ /MP_verify_... # ^~ ensures this path is matched with highest priority.
add_header Content-Type "text/plain; charset=utf-8"; # Sets response type and encoding.
default_type text/plain; # Sets default MIME type (optional but recommended).
return 200 "pydasYvevk9k84q7"; # Returns status 200 and the verification string without a real file.Tip: Each verification file requires a separate location block. For multiple WeChat apps, add multiple blocks.
3. Verify Effectiveness
Reload Nginx and test:
sudo nginx -t && sudo nginx -s reload
curl https://yourdomain.com/MP_verify_pydasYvevk9k84q7.txtIf the response is exactly pydasYvevk9k84q7 (no extra spaces or line breaks), the configuration is successful.
4. Frequently Asked Questions
Q1: Why not use alias or root to point to a real file?
Real files need manual maintenance and are prone to deletion.
Automation requires extra file distribution steps.
Returning content directly from Nginx is lighter, safer, and more controllable.Q2: Must Content-Type be text/html?
WeChat does not enforce it; both text/plain and text/html work, but text/plain is recommended.
Q3: Can multiple verification files be configured in batch?
location ^~ /MP_verify_abc123.txt {
return 200 "abc123";
}
location ^~ /MP_verify_xyz789.txt {
return 200 "xyz789";
}5. Conclusion
Using Nginx's return directive allows elegant handling of WeChat verification files without storing any static files on the server, making the process concise, efficient, and less error‑prone for WeChat service deployments.
Xiao Liu Lab
An operations lab passionate about server tinkering 🔬 Sharing automation scripts, high-availability architecture, alert optimization, and incident reviews. Using technology to reduce overtime and experience to avoid major pitfalls. Follow me for easier, more reliable operations!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
