0

Nginx は初めてなので (私は本当にルーキーです)、.htaccess URL の書き換えが Nginx で機能しないことがわかりました。

ここでいくつかのコンバーターを見つけました: http://winginx.com/htaccess

そこに.htaccessを変換しようとしましたが、i 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 に答える