nginx rewrite

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx rewrite相关的知识,希望对你有一定的参考价值。

apache和nginx区别主要在pathinfo的处理, 直接拿去用:

location / { 

    if (!-e $request_filename) { 

       rewrite ^(.*)$ /index.php?$1 last;  break; 

     } 

}

附上本人的配置文件:

server {

listen 80;

server_name redis.yanjiadong.net;

index index.php index.html;

root /alidata/www/redis;

location / {

if (!-e $request_filename) {

rewrite ^(.*)$ /index.php?s=$1 last; break;

}

}

location ~ .*\.(php|php5)?$

{

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{
expires 30d;

}

location ~ .*\.(js|css)?$

{
expires 1h;

}
access_log /data/log/access/redis.log;
}

点法的nginx.conf:

server{

        listen 80;

        server_name  wiki.idianfa.com ;

        root /alidata/app/project/application/;

        index index.html index.htm index.shtml index.php;


#       error_page  404               /404.html;

    #Custom rules Start

    if (-f $request_filename/index.html){

rewrite (.*) $1/index.html break;

}

if (-f $request_filename/index.php){

rewrite (.*) $1/index.php;

}

if (!-f $request_filename){

rewrite (.*) /index.php;

}    #Custom rules End

    location = /500.html {

        root   /usr/share/nginx/html;

    }


    location ~ \.php$ {

        fastcgi_pass   unix:/dev/shm/php.sock;

        include        fastcgi_params;

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

       # access_log     /a/apps/nginx/logs/idianfa.com.access.log main;

    }

        location ~ .*\.(js|css)?$

        {

                expires 1d;

        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)?$

        {

                expires 15d;

        }

    location ~ /\.ht {

        deny  all;

    }

}




再来一发:

user  nginx;

worker_processes  1;

worker_rlimit_nofile 65535;

events {

        use epoll;

        worker_connections 65535;

}


http {

    include       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 /a/apps/nginx/logs/access.log  main;

        error_log  /a/apps/nginx/logs/error.log warn;


    sendfile            on;

        send_timeout        120s;

    tcp_nopush          on;

    tcp_nodelay         on;

    server_tokens       off;

    keepalive_timeout   360s;

    keepalive_requests  1000;


    gzip                on;

    gzip_min_length     1k;

    gzip_buffers        4 16k;

    output_buffers      1 512k;

    postpone_output     1460;

    gzip_comp_level     4;

    gzip_vary           on;


        fastcgi_connect_timeout      300;

        fastcgi_send_timeout         300;

        fastcgi_read_timeout         300;

        fastcgi_buffer_size          512k;

        fastcgi_buffers              8 512k;

        fastcgi_busy_buffers_size    512k;

        fastcgi_temp_file_write_size 512k;

    fastcgi_intercept_errors     on;


        server_names_hash_bucket_size 128;

        client_header_buffer_size     32k;

        large_client_header_buffers   4 32k;

        client_max_body_size          100m;

        client_header_timeout         120s;

    client_body_timeout           120s;


        proxy_connect_timeout      60;

        proxy_read_timeout         60;

        proxy_send_timeout         60;

 proxy_buffer_size          64k;

        proxy_buffers              64 64k;

        proxy_busy_buffers_size    128k;

        proxy_temp_file_write_size 128k;


    ssi on;

        ssi_silent_errors on;

        ssi_types text/shtml;


    server {

            listen       80 default;

            server_name  "";


            root /a/apps/zhujibao/manager/linuxdef;

                index index.html index.htm index.shtml index.php;


                location ~ \.php$ {

                        fastcgi_pass   unix:/dev/shm/php.sock;

                        include        fastcgi_params;

                        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

                }

        }


    server{

                listen  9999;

                server_name x.admin5.com;

                root /a/apps/zhujibao/manager/public;

                index index.html index.htm index.shtml index.php;


                if (!-e $request_filename) {

                        rewrite ^.*$ /index.php last;

                }


                location ~ \.php$ {

                        fastcgi_pass   127.0.0.1:9001;

                        include        fastcgi_params;

                        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

                        access_log     /a/apps/nginx/logs/zhujibao.access.log main;

                }


                location ~ /\.ht {

                        deny  all;

                }

        }


        include vhosts/*.conf;

}




以上是关于nginx rewrite的主要内容,如果未能解决你的问题,请参考以下文章

Nginx Rewrite重写

Nginx Rewrite重写

Nginx Rewrite重写

Nginx rewrite重写详解 #yyds干货盘点#

nginx location Rewrite 乱七八糟 农贷

Nginx前端域名重定向跳转至另一个域名