将规则从 apache 转换为 nginx

将规则从 apache 转换为 nginx

我对这些规则有疑问:

ErrorDocument 500 http://example.com/500.html
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]

谁能帮我?

答案1

指示

ErrorDocument 500 http://example.com/500.html

转换为

error_page 500 http://example.com/500.html;



将 www 重写为非 www 服务器名称

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

转换为本server节:

server {
    server_name  www.example.com;
    rewrite ^ http://example.com$request_uri?;
}



并重写块

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

变成

try_files $uri $uri/ /index.php$request_uri?;



因此最终的 nginx 配置将如下所示:

server {
    listen 80;
    server_name  www.example.com;
    rewrite ^ http://example.com$request_uri?;
}

server {
    listen 80;
    server_name  example.com;

    root /var/www/example.com;
    error_page 500 http://example.com/500.html;

    location / {
        index index.php;
        try_files $uri $uri/ /index.php$request_uri?;   
    }

    location ~ \.php$ {
        #
        # PHP processing here
        #
    }       
}

相关内容