Nginx重写协助请求

时间:2014-01-10 17:50:46

标签: nginx rewrite

总而言之,我有一个osCommerce安装,使用osCommerce的“搜索引擎友好链接”已在Apache服务器上运行多年,因此链接看起来像product_info.php / products_id / 26而不是product_info。 PHP?products_id = 26

好吧,现在我已经转移到使用PHP5的Nginx服务器,而搜索引擎友好链接现在对我不起作用。但我在网上指向我的商店使用“友好”链接的链接,所以我必须想办法重定向这样的网址:

product_info.php/products_id/26

进入这个

product_info.php?products_id=26

index.php/cPath/19

index.php?cPath=19

product_info.php/cPath/19/products_id/207

product_info.php?cPath=19&products_id=207

任何善于使用Nginx重写规则的人,或者谁知道如何使osCommerce内置SEO网址与Nginx合作? osCommerce网址不需要任何htaccess重写规则;看起来好像都在使用PHP代码。

nginx.conf:

user  nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server_names_hash_bucket_size 10240;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-available/*.vhost;

}

site.com.vhost:

server {
        listen *:80;
        server_name site.com www.site.com;
        root   /var/www/clients/client23/web5/web;
        index index.php index.html index.htm;

        #location ~ \.shtml$ {
        #    ssi on;
        #}

        error_log /var/log/ispconfig/httpd/site.com/error.log;
        access_log /var/log/ispconfig/httpd/site.com/access.log combined;

        location ~ /\. {
            deny all;
            access_log off;
            log_not_found off;
        }

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

        location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
        }

        location /stats {
            index index.html index.php;
            auth_basic "Members Only";
            auth_basic_user_file /var/www/clients/client23/web5/web/stats/.htpasswd_stats;
        }

        location ^~ /awstats-icon {
            alias /usr/share/awstats/icon;
        }

        location ~ \.php$ {
            try_files /5lbe4fd76b7f89.htm @php;
        }

        location @php {
            try_files $uri =404;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/lib/php5-fpm/web2.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
        }



  # Static Contents
  location ~* ^.+.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf)$ {
    access_log off;
    log_not_found off;                
    expires 1y;                
  }
  # CSS and JS
  location ~* ^.+.(css|js)$ {
    access_log off;
    log_not_found off;                
  }

  server_tokens off;
  error_page 400 /400.php;
  error_page 401 /401.php;
  error_page 403 /403.php;
  error_page 404 /404.php;
  error_page 405 /405.php;
  error_page 500 /500.php;
  error_page 502 /502.php;
  error_page 503 /503.php;
  recursive_error_pages off;
  fastcgi_intercept_errors on;

  # Inaccessible locations
location ~ ^/includes/.*\.php$ { return 404; }
location ~ ^/administrator/includes/.*\.php$ { return 404; }
location ^~ /administrator/backups { return 404; }
location ^~ /download { return 404; }
location ^~ /cgi-bin { return 404; }
location ^~ /mail { return 404; }
location ^~ /pub { return 404; }
location ^~ /sql { return 404; }
location ^~ /temp { return 404; }

location ~ /\. { deny all; access_log off; log_not_found off; }
location ~ \.(tpl|log|sql)$ {deny all; access_log off; log_not_found off; }

location / {

    #if (!-e $request_filename) { rewrite ^(.*)$ /index.php; }

        fastcgi_pass unix:/var/lib/php5-fpm/web2.sock;
    fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;


    rewrite ^/(.*)-(.*).html$ /index.php?cPath=$2&$query_string;
    rewrite ^/(.*)-m-([0-9]+).html$ /index.php?manufacturers_id=$2&$query_string;
    rewrite ^/(.*)-pi-([0-9]+).html$ /popup_image.php?pID=$2&$query_string;
    rewrite ^/(.*)-t-([0-9]+).html$ /articles.php?tPath=$2&$query_string;
    rewrite ^/(.*)-a-([0-9]+).html$ /article_info.php?articles_id=$2&$query_string;
    rewrite ^/(.*)-pr-([0-9]+).html$ /product_reviews.php?products_id=$2&$query_string;
    rewrite ^/(.*)-pri-([0-9]+).html$ /product_reviews_info.php?products_id=$2&$query_string;
    rewrite ^/(.*)-i-([0-9]+).html$ /information.php?info_id=$2&$query_string;
  }

        location /administrator/ {
                auth_basic "Members Only";
                auth_basic_user_file /var/www/clients/client23/web5/web/administrator/.htpasswd;

                location ~ \.php$ {
                    try_files /wj70wjksdkj2jiejlsjdslj.htm @php;
                }
        }

}

1 个答案:

答案 0 :(得分:0)

我们走了:

rewrite "^/product_info.php/products_id/(\d+)" /product_info.php?products_id=$1 permanent;

rewrite "^/index.php/cPath/(\d+)" /index.php?cPath=$1 permanent;

rewrite "^/product_info.php/cPath/(\d+)/products_id/(\d+)" /product_info.php?cPath=$1&products_id=$2 permanent;