web-dev-qa-db-ja.com

NGINX書き換えの質問

次のApacheルールをどのように変換しますか

RewriteRule ^watch/([A-Za-z0-9-]+)/?$ watch.php?v=$1 [NC]

nGINX構成ファイルの適切な形式に変換しますか?

さらに編集:

server {
    listen 80;
    listen [::]:80;

    server_name domain.tld www.domain.tld;
    root /var/www/ROOT;
    index index.php index.html index.htm;

        return 302 https://www.domain.tld$request_uri;

    location / {
        #
        # HIDE PHP EXTENSION
        #
        try_files $uri $uri/ @extensionless-php;
    }


    location /watch.php {
        rewrite ^/watch/([A-Za-z0-9-]+)/?$ /watch.php?v=$1;
    }

    #
    # HIDE PHP EXTENSION
    #
    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }

    #
    # BLOCK REFERRER URLS
    #

    location ~ \.php$ {
        try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    error_page 401 /error_pages/401.php;
    error_page 403 /error_pages/403.php;
    error_page 404 /error_pages/404.php;
    error_page 405 /error_pages/405.php;
    error_page 408 /error_pages/408.php;

    location ^~ /error/ {
        internal;
        root /error_pages/;
    }
2
itteam

nginxのURIには先頭の/が含まれているため、同等の書き換えは次のようになります。

location /watch/ {
    rewrite ^/watch/([A-Za-z0-9-]+)/?$ /watch.php?v=$1 last;
}

それはlocation/{}ブロックに入れますか?

これはもう少し複雑な質問であり、nginxの構成、特に他にどのlocationブロックが存在するかによって異なります。

オプションは次のとおりです。

  • serverコンテナの上部(locationブロックの外側)の近くに配置します
  • /の場所に置きます
  • /watchの場所に置きます
2
Richard Smith