nginx配置rewrite

1. uri  和 url读取区别

区别就是URI定义资源,而URL不单定义这个资源,还定义了如何找到这个资源。

比如说,一个服务器上,到一个文件夹/网页的绝对地址(absolute path)就是URI。

Nginx的rewirte是针对 uri的 不是url.

2. location的使用

    location / {
        rewrite ^.*$ /index.php last;
    }
# /是通用的目录  所有没有匹配的rewite的最后都会用/匹配
    location ~ ^/asset/ {
            root /home/users/work/webroot/public/fe;
    }
#~ 开头表示区分大小写的正则匹配    这里是匹配下一级目录/asset/开头
#~*  开头表示不区分大小写的正则匹配
#!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

    location ~ ^/dep/ {
            root /home/users/work/webroot/public/fe;
    }
# dep同上, 匹配/dep/目录, 当访问/dep目录时, 网站的根目录会变为
fe
    location ~ ^/page/.*/asset-\d+/ {
            root /home/users/work/webroot/public/fes;
    }
# 此处为匹配page目录下asset后有数字的目录,/page/目录时, 网站的根目录会变为fes

    location ~\.php$ {
        root            /home/users/work/webroot/public;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_param   SCRIPT_FILENAME        $document_root$fastcgi_script_name;
        fastcgi_index   index.php;
        include         fastcgi.conf;
    }
#以上结果都不匹配, location是.php结尾,注意这里是\不是/ 用来转义.的, $是以.php结尾的意思, 这里就把所有的.php的请求都重定向到/home/users/work/webroot/public 下, 并且使用fastcgi协议通过127.0.0.1:9000转发请求,  fastcgi_param定义各个参数, 具体含义如下表,  这里的$document_root$fastcgi_script_name 就是/home/users/work/webroot/public/index.php 请求脚本的路径和名称

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径
fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123
fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)
fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段
fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。


fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称
fastcgi_param REQUEST_URI $request_uri; #请求的地址不带参数
fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。
fastcgi_param DOCUMENT_ROOT $document_root; #网站的根目录。在server配置中root指令中指定的值
fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。


fastcgi_param GATEWAY_INTERFACE CGI/1.1;#cgi 版本
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;#nginx 版本号,可修改、隐藏


fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP
fastcgi_param REMOTE_PORT $remote_port; #客户端端口
fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址
fastcgi_param SERVER_PORT $server_port; #服务器端口
fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name


#fastcgi_param PATH_INFO $path_info;#可自定义变量


# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;


在php可打印出上面的服务环境变量
如:echo $_SERVER[‘REMOTE_ADDR‘]


还见过一个简便的使用框架的写法,所有的location凡是uri指定的文件不存在则重定向到 /index.php?s=$uri上。
location / { 

    if (!-e $request_filename) { 

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

        break;   

    } 

}
时间: 2024-08-03 01:08:39

nginx配置rewrite的相关文章

nginx 配置rewrite 笔记

nginx 配置rewrite笔记: 通过下面的示例来说明一下,1. 先说说location : location 表示匹配传入的url地址,其中配置符有多种,各种情况的意义不一样: location ^~ /public/ { root /data/wwwroot/a.php.abc.cc; } location ^~ /public/ 表示匹配以 "/public/" 开头的url,匹配成功执行其中的内容,执行完毕后停止并退出. location / { root /data/ww

nginx 配置rewrite

先说自己的情况,目前富乔使用的是lnmp一键包,解决步骤如下: 1.打开/usr/local/nginx/conf/nginx.conf   文件,在server段中,access_log句子前加入以下代码 location /ck/cashier/ { if (!-e $request_filename){ rewrite ^/ck/cashier/(.*)$ /ck/cashier/index.php?s=/$1 last; } } 其中/ck/cashier/  为二级目录,可根据自己的项

Nginx配置rewrite过程介绍

创建rewrite语句 vi conf/vhost/www.abc.com.conf #vi编辑虚拟主机配置文件 文件内容 server { listen 80; server_name abc.com; rewrite ^/(.*) http://www.abc.com/$1 permanent; } server { listen 80; server_name www.abc.com; location / { root /data/www/www; index index.html in

【Nginx】Nginx配置REWRITE隐藏index.php

只需要在server里面加上 if ( !e $request_filename ) { rewrite ^/(.*)$ /index.php/$1 last; } 原文地址:https://www.cnblogs.com/BearLee/p/9520794.html

nginx配置访问控制、rewrite应用、nginx代理

一.访问控制 限制只让某个ip访问: allow 192.168.1.100; deny all; 限制只有本地地址可以访问,白名单: allow 127.0.0.1; deny all; 拒绝本地访问,黑名单: deny 127.0.0.1; allow all; deny all 直接拒绝所有,下面的allow就不生效了. [[email protected] vhosts]# vi default.conf server {     listen 80 default_server;   

Nginx配置的rewrite编写时last与break的区别详解

rewite 1. server块中的rewrite: 在server块下,会优先执行rewrite部分,然后才会去匹配location块 server中的rewrite break和last没什么区别,都会去匹配location,所以没必要用last再发起新的请求,可以留空. 2. location中的rewirte: 不写last和break -    那么流程就是依次执行这些rewrite 1. rewrite break -        url重写后,直接使用当前资源,不再执行loca

nginx配置location及rewrite规则重写

一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 # 但是正则和最长字符串会优先匹配 [ configuration B ] } location /documents/ { # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ c

nginx的rewrite配置

域名跳转(重定向).URL重写(伪静态).动静分离(跳转域名,并接入CDN实现加速)#依赖PCRE库#模块:ngx_http_rewrite_moduleRwrite相关指令#if (条件) { command } coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md#break和last coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md#retu

Nginx配置详解

Nginx配置文件主要分成四部分: main(全局设置)指令将影响其它所有部分的设置: server(主机设置)指令主要用于指定虚拟主机域名.IP和端口: upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡: location(URL匹配特定位置后的设置),每部分包含若干个指令.location部分用于匹配网页位置(比如,根目录"/","/images",等等). 他们之间的关系式: