nginx 完美支持tp路由配置

server {
        listen       80;
        root /www/web/fubao/serve/public;
        server_name asfserve.bjfable.com;
        index  index.html index.php index.htm;
        error_page  400 /errpage/400.html;
        error_page  403 /errpage/403.html;
        error_page  404 /errpage/404.html;
        error_page  405 /errpage/405.html;
        error_page  503 /errpage/503.html;
        location ~ \.php(.*)$ {
                fastcgi_pass  unix:/tmp/php-71-cgi.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
                fastcgi_param PATH_INFO $2;
           
#下面两句是给fastcgi权限,可以支持 ?s=/module/controller/action的url访问模式
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

    
		
#下面两句才能真正支持 index.php/index/index/index的pathinfo模式
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;



		 #include fcgi.conf;
        }


location / {

            index  index.html index.htm index.php;
            #autoindex  on;
           

          if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
            break;
          }
        }

}

 

Nginx配置路由可以通过location指令来实现。下面是一个简单的例子: ``` server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } ``` 这个例子中,如果有请求访问http://example.com,Nginx会将请求路由到后端应用服务器的8080端口上。 解释一下这个配置: - listen 80:监听80端口,表示处理HTTP请求。 - server_name example.com:Nginx会处理所有来自example.com域名的请求。 - location /:这是路由配置,表示将所有请求都路由到后端应用服务器。 - proxy_pass http://127.0.0.1:8080:将请求转发到127.0.0.1:8080,即后端应用服务器的地址和端口。 - proxy_set_header Host $host:设置请求头中的Host字段,确保后端应用服务器可以正确识别请求的域名。 - proxy_set_header X-Real-IP $remote_addr:设置请求头中的X-Real-IP字段,记录请求的真实IP地址。 另外,还可以根据需要进行更复杂的路由配置,比如: ``` server { listen 80; server_name example.com; location /api/ { proxy_pass http://127.0.0.1:8080; ... } location / { root /var/www/example.com; ... } } ``` 这个例子中,如果有请求访问http://example.com/api/,Nginx会将请求路由到后端应用服务器的8080端口上;如果有请求访问http://example.com/,Nginx会将请求路由到本地文件系统的/var/www/example.com目录下。 以上是在Nginx配置路由的方法和示例。需要注意的是,路由配置的正确性和复杂性需要根据具体情况进行考虑和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值