You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache迁移Nginx后SEO重写规则引发504网关超时问题求助

Troubleshooting Nginx Rewrite Loops & 504 Timeouts When Migrating from Apache

Hey Jignesh! Let's tackle that Nginx rewrite loop and 504 timeout issue you're hitting after migrating from Apache. I’ve seen this exact problem dozens of times, so let’s break down what’s going wrong and how to fix it.

Why You’re Seeing Loops & Timeouts

9 times out of 10, this happens because your Nginx rewrite rule isn’t properly excluding existing files/directories, or it’s creating a circular redirect between the rewritten URL and the original pattern. Unlike Apache, Nginx handles rewrites and request routing differently—direct translations often cause unexpected behavior.

Let’s Start with Common Example Rules

First, let’s use a typical Apache SEO rewrite rule (I’ll assume yours looks something like this):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

If you translated this directly to Nginx without accounting for Nginx’s logic, you might end up with a rule like this that causes endless loops:

rewrite ^/(.*)$ /index.php/$1;

The Fix: Use try_files (Nginx’s Preferred Method)

Nginx’s try_files directive is the efficient, loop-proof way to handle SEO-friendly URLs. Here’s the corrected configuration tailored to most PHP-based apps:

# Main routing block
location / {
    # First check if the request is for an existing file or directory
    try_files $uri $uri/ @rewrite;
}

# Named location for rewriting non-existent paths
location @rewrite {
    # Rewrite the path to pass to index.php, use 'last' to avoid loops
    rewrite ^/(.*)$ /index.php/$1 last;
}

# Critical: PHP handling block with path info support
location ~ \.php(/|$) {
    fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Adjust to your PHP-FPM socket/path
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    **fastcgi_param PATH_INFO $fastcgi_path_info;** # This is key for passing the route to your app
}

Why This Fixes the Problem

  • try_files first verifies if the requested URI is an actual file ($uri) or directory ($uri/). Only if it doesn’t exist does it trigger the rewrite—this prevents unnecessary rewrites of valid assets.
  • The last flag tells Nginx to stop processing rewrite rules in the current block and start fresh with the rewritten URL. This breaks any circular loop that would otherwise cause timeouts.
  • The PHP block explicitly passes PATH_INFO to your application, ensuring it receives the correct SEO route without additional redirects.

Extra Steps to Eliminate 504 Timeouts

  • Adjust FastCGI timeouts if your app needs more time to process requests:
    fastcgi_connect_timeout 60s;
    fastcgi_send_timeout 60s;
    fastcgi_read_timeout 60s;
    
  • Tune Nginx worker settings in your main nginx.conf to handle traffic load:
    worker_processes auto; # Uses all available CPU cores
    worker_connections 1024; # Adjust based on your server’s capacity
    
  • Check for conflicting rules in other server blocks or included config files—overlapping rewrites can create hidden loops.

If your specific Apache/Nginx rules differ from these examples, share them and I’ll refine the fix further!


内容的提问来源于stack exchange,提问作者Jignesh Patgir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:18:39