ホームページ >バックエンド開発 >PHPチュートリアル >Nginx で URL 書き換えを有効にする方法

Nginx で URL 書き換えを有効にする方法

WBOY
WBOYオリジナル
2016-06-23 14:23:31984ブラウズ

本帖最后由 yxqc 于 2013-10-07 11:58:51 编辑

我的Nginx.conf如下:
#user  nobody;worker_processes  1;error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}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  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    server {        listen       8088;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   D:/CwsServ/www;            index  index.php index.html index.htm ;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        location ~ \.php {            root           D:/CwsServ/www;            fastcgi_pass   127.0.0.1:9000;            fastcgi_index  index.php;            fastcgi_param  SCRIPT_FILENAME  D:/CwsServ/www$fastcgi_script_name;            include        fastcgi_params;						#pathinfo support             set $real_script_name $fastcgi_script_name;             set $path_info "";             if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$")			{                 set $real_script_name $1;                 set $path_info $2;             }               fastcgi_param SCRIPT_NAME $real_script_name;             fastcgi_param PATH_INFO $path_info;         }        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443;    #    server_name  localhost;    #    ssl                  on;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_timeout  5m;    #    ssl_protocols  SSLv2 SSLv3 TLSv1;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers   on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

回复讨论(解决方案)

http://wiki.nginx.org/HttpRewriteModule

nginx的文档写得很全面了

http://wiki.nginx.org/HttpRewriteModule

nginx的文档写得很全面了

不知我的nginx.conf要怎么修改???

有人知道不?

编译时不指定--without-http_rewrite_module默认就是开启的。


使用rewrite指令来完成url重写,rewrite指令只适用于server、location和if域中,其它域中不支持。


例子:

server{
        listen       80;
        server_name test.angryfrog.com;
        root /Users/angryfrog/website/test;
        index index.php;
        
        if (!-f $request_filename){
            rewrite ^/(.+)$ /index.php?$1& last;
        }
}

此站点下请求的如果请求的文件不存在,则将会重定向到根目录下得index.php文件,并将请求的内容作为参数传给index.php文件。

参考文章: nginx url重写-rewrite实例
nginx rewrite(nginx url地址重写)的配置示例
超详细的 NGINX URL重写实例讲解

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。