Converter as diretivas mod_rewrite do HTACCESS para o formato nginx?

2

Sou novo no nginx e estou tentando converter o aplicativo que eu escrevi do Apache, já que preciso da capacidade de atender a muitos clientes de uma só vez sem muita sobrecarga! Eu estou pegando o jeito de configurar nginx e FastCGI PHP, mas eu não posso enrolar minha cabeça em torno do formato de reescrita do nginx ainda. Eu sei que você tem que escrever algum script simples que vai no servidor {} block na configuração nginx mas eu ainda não estou familiarizado com a sintaxe. Alguém com experiência em Apache e nginx pode me ajudar a converter isso para o formato nginx? Obrigado!

# ------------------------------------------------------ #
# Rewrite from canonical domain (remove www.)            #
# ------------------------------------------------------ #

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

# ------------------------------------------------------ #
# This redirects index.php to /                          #
# ------------------------------------------------------ #

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index|index\.php)\ HTTP/
    RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L] 

# ------------------------------------------------------ #
# This rewrites 'directories' to their PHP files,        #
# fixes trailing-slash issues, and redirects .php        #
# to 'directory' to avoid duplicate content.             #
# ------------------------------------------------------ #

    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*)$ $1.php [L]

    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*)/$ http://domain.com/$1 [R=301,L]

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.]+\.php\ HTTP/
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^.]+)\.php$ http://domain.com/$1 [R=301,L]

# ------------------------------------------------------ #
# If it wasn't redirected previously and is not          #
# a file on the server, rewrite to image generation      #
# ------------------------------------------------------ #

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$ generation/image.php?user=${escapemap:$1}&template=${escapemap:$2} [NC,L]
    
por Chris 10.08.2010 / 02:31

1 resposta

3

Eu converto part-by-part para seguir mais facilmente.

Apache:

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

Nginx:

server {
    listen 80;
    server_name www.domain.com;
    root /var/www/localhost/htdocs;

    rewrite ^ http://domain.com$request_uri permanent;
}

Apache:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index|index\.php)\ HTTP/
RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L] 

Nginx:

location = /index {
    try_files $uri @homepage;
}
location @homepage {
    rewrite ^ http://domain.com break;
}
location ~ \.php$ {
    if ($request_uri ~* /index\.php$) {
        rewrite ^ http://domain.com break;
    }
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi.conf;

    fastcgi_intercept_errors        on;
    error_page 404 /error/404.php;
}

Apache:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/$ http://domain.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.]+\.php\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)\.php$ http://domain.com/$1 [R=301,L]

Nginx:

rewrite ^/(.*)/$ /$1 permanent;

if (-f $document_root/$request_uri.php) {
    rewrite ^(.*)$ $1.php last;
}

location ~ \.php$ {
    if (-f $document_root/$request_uri) {
        rewrite ^([^.]+)\.php$ http://domain.com$1 permanent;
    }
    ...
}
    
por 27.09.2011 / 09:49