0

作为 Nginx 的新手(我真的是一个菜鸟),我刚刚发现我的 .htaccess url 重写不适用于 Nginx。

我在这里找到了一些转换器: http ://winginx.com/htaccess

我尝试在那里转换我的 .htaccess,但是在集成我的 nmy .conf 之后,结果出现了一个奇怪的结果,即在单击它们时提示下载一些 url 等。

也许我误解了如何集成它,但我所做的是在 .conf 中为我的特定站点定义它,即:

/etc/nginx/sites-available/mydomain.conf

我原来的 .htaccess 如下所示:

#php_value arg_separator.output &

Options +FollowSymlinks
RewriteEngine on

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

RewriteRule ^([^/\.]+)/?$ index.php?cat=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ picture-normal.php?pic=$2 [L]
#RewriteRule ^([^/\.]+)/([^/\.]+)/(.*)$ ./$3 [L]

#RewriteRule ^[^/]/([0-9]*) test.html?id=$1

#RewriteRule ^set([0-9]*)-Assorted_Images_([0-9\-]*).html set.php?id=$1
#RewriteRule ^Funny-(.*)-([0-9]*).html jokes.php?id=$2
#RewriteRule ^Funny-(.*)-([0-9]*)-page([0-9]*).html jokes.php?id=$2&page=$3
#RewriteRule ^funny-joke-([0-9]*)-(.*).html joke.php?id=$1

#RewriteRule ^(.*).html index.php?p=$1

其中转换为:

    # nginx configuration

    location / {
      if ($http_host !~ "^www\.mydomain\.com$"){
        rewrite ^(.*)$ http://www.mydomain.com/$1 redirect;
      }
      rewrite ^/([^/\.]+)/?$ /index.php?cat=$1 break;
      rewrite ^/([^/\.]+)/([^/\.]+)/?$ /picture-normal.php?pic=$2 break;
    }

整个 .conf 看起来像这样: /etc/nginx/sites-available/mydomain.conf :

server
{
    server_name .mydomain.com;

    access_log /var/log/nginx/mydomain.com.access.log;

        error_log /var/log/nginx/mydomain.com.error.log;

    root /var/www/mydomain.com/html;

    index index.php index.html index.htm;

    # use fastcgi for all php files
    location ~ \.php$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }

    # nginx configuration

    location / {
      if ($http_host !~ "^www\.mydomain\.com$"){
        rewrite ^(.*)$ http://www.mydomain.com/$1 redirect;
      }
      rewrite ^/([^/\.]+)/?$ /index.php?cat=$1 break;
      rewrite ^/([^/\.]+)/([^/\.]+)/?$ /picture-normal.php?pic=$2 break;
    }
}
4

1 回答 1

0

如果您确实正确设置了 PHP fastcgi,那么这应该对您有用。

server {
    listen 80;
    server_name mydomain.com;
    return       301 http://www.mydomain.com$request_uri;
}
server {
    listen 80;
    server_name www.mydomain.com;
    access_log /var/log/nginx/mydomain.com.access.log;
    error_log /var/log/nginx/mydomain.com.error.log;
    root /var/www/mydomain.com/html;
    index index.php index.html index.htm;

    # use fastcgi for all php files
    # Are you sure you have this set up?
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to apache .htaccess files
    location ~ /\.ht {
        deny all;
    }

    # nginx configuration
    location / {
        rewrite ^([^/\.]+)/?$ index.php?cat=$1 last;
        rewrite ^([^/\.]+)/([^/\.]+)/?$ picture-normal.php?pic=$2 last;
    }
}

主要区别是在单独的服务器块中执行 mydomain 到 www.mydomain 的重定向,这更有效,并将“breaks”替换为“last”,如果 PHP fastcgi 设置正确,将修复下载问题。

于 2012-12-27T15:41:02.327 回答