【问题标题】:Change format from htaccess to nginx将格式从 htaccess 更改为 nginx
【发布时间】:2015-12-01 21:57:45
【问题描述】:

我有这样的 htaccess 文件的 apache 服务器:

RewriteCond %{HTTP_HOST}  ^.*$ [NC]
RewriteRule   ^test/\$([^/]+)/([^/]+\.php)$  test/$2?VIRTUAL_DIRECTORY_NAME=$1 [L,QSA]

RewriteCond %{HTTP_HOST}  ^.*$ [NC]
RewriteRule   ^test/([^/]+)/(.*)$  $1/$2 [L]

我正在尝试将其转换为 nginx 服务器。 我不知道如何将虚拟目录转换为正确的格式。 这是我的 nginx 配置文件:

server {
    listen 80;
    listen 443 ssl;
    server_name admin.dev;
    root "/home/vagrant/admin";

    index index.html index.htm index.php;

    charset utf-8;


    rewrite_log on;

    location /api/ {
        rewrite ^/api/(.*)$ /api.php?$1 last;
    }

    location /res_partners/ {
        rewrite ^res_partners/\$([^/]+)/([^/]+\.php)$  res_partners/$2?VIRTUAL_DIRECTORY_NAME=$1 last;
    }


    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/admin.dev-error.log error;

    sendfile off;

    client_max_body_size 100m;

   location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }   

    location ~ /\.ht {
        deny all;
    }   

    ssl_certificate     /etc/nginx/ssl/admin.dev.crt;
    ssl_certificate_key /etc/nginx/ssl/admin.dev.key;
}

有什么帮助吗? 谢谢

【问题讨论】:

    标签: .htaccess nginx


    【解决方案1】:

    第一条规则似乎将/test/$something/somescript.php 重写为/test/somescript.php?VIRTUAL_DIRECTORY_NAME=$something,这些是文字$ 字符。您的 location ~ \.php$ { ... } 容器已经匹配任何以 .php 结尾的 URI。所以第一条规则可以放在那个容器的顶部:

    location ~ \.php$ {
      rewrite ^/test/\$([^/]+)/([^/]+\.php)$ /test/$2?VIRTUAL_DIRECTORY_NAME=$1&$args break;
      ...
    }
    

    第二条规则似乎将/test/somedir/something 重写为/somedir/something(其中something 不是php 脚本)。

    这可以通过前缀位置来实现:

    location /test/ {
      rewrite ^/test/([^/]+)/(.*)$ /$1/$2;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-13
      • 1970-01-01
      • 2017-01-30
      • 2014-09-19
      • 2015-11-11
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多