09_Nginx配伪静态写法


title: Nginx配伪静态写法
urlname: 09_Nginx配伪静态写法
date: 2017-04-28 03:03:03
tags: [nginx]
categories: 网络安全

Nginx配伪静态写法
伪静态是一种可以把文件后缀改成任何可能的一种方法,如果我想把PHP文件伪静态成html文件,这种相当简单的,下面来介绍nginx 伪静态配置方法有需要了解的朋友可参考。
nginx里使用伪静态是直接在nginx.conf 中写规则的,并不需要像apache要开启写模块(mod_rewrite)才能进行伪静态。
nginx只需要打开nginx.conf配置文件,在server里面写需要的规则即可。

就用上面的例子中的伪静态策略来简单解释下正则表达式在配置的作用:
策略:RewriteRule ^(.*)list-([0-9]+)-([0-9]+).html$ $1list.php?page=$2&id=$3
请求路径:http://www.abc.com/list-123-456.html

本策略分成两段,第一段是使用正则表达式去匹配请求访问的路径,第二段是将匹配后的参数转化为真实访问的路径。策略执行时:^(.*)list-([0-9]+)-([0-9]+).html$ 与 /list-123-456.html 这个字符串进行匹配:
^和$字符分别代表了匹配输入字符串的开始和结束
()中的匹配到的内容会被按顺序分配到变量$1 $2 $3中
.*匹配任意字符串,且长度从0个到多个,故$1值为/
[0-9]+匹配字符0-9,长度1个到多个,故$2和$3分别是123和456

所以最后真实访问的动态地址为 /list.php?page=123&id=456
上面只是介绍了基本的配置策略,其实这个rewrite规则还有非常多的玩法,例如使用RewriteCond进行域名层面的重写跳转,接下来可以找些资料慢慢补充。

scc.gd配置实例:

user  www www;

worker_processes auto;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

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

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 600;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        access_log off;

server
    {
        listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;
        server_name scc.gd;
        index index.html index.htm index.php;
        root  /home/wwwroot/scc.gd;

        #error_page   404   /404.html;
        include enable-php.conf;

        if ($document_uri = '/api.php'){
            rewrite ^(.*)/api.php?(.*)$ $1/index.php?m=Index&a=api&$2 last;
        }

        if (!-d $document_uri){
            rewrite ^/([0-9a-zA-Z]+)$ /index.php?m=Index&a=jump&u=$1 last;
        }

        if ($host != 'scc.gd') {
            rewrite ^/(.*)$ http://scc.gd/$1 permanent;
        }

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /home/wwwlogs/access.log;
    }
include vhost/*.conf;
}


xsser平台的nginx规则

root@www:/usr/local/nginx/conf# cat xss.conf 
rewrite "^/([0-9a-zA-Z]{4})$" /xss.php?do=code&urlKey=$1 last;
rewrite "^/([0-9a-zA-Z]{6})$" /xss.php?do=code&urlKey=$1 break;
rewrite "^/do/auth/(w+?)(/domain/([w.]+?))?$" /xss.php?do=do&auth=$1&domain=$3 break;
rewrite "^/register/(.?)$" /xss.php?do=register&key=$1 break;
rewrite "^/register-validate/(.?)$" /xss.php?do=register&act=validate&key=$1 break;
rewrite "^/login$" /xss.php?do=login break;

一些常用内置变量

$arg_name:请求 uri 中的 name 参数至
$args:请求 uri 的所有参数,和 $query_string 相同
$uri:当前请求的 uri,不带参数
$request_uri:请求的 uri,带完整参数
$host:http 请求报文中 host 首部,如果没有 host 首部,则以处理此请求的虚拟主机的主机名替代
$hostname:nginx 服务运行在主机的主机名
$remote_addr:客户端 IP
$remote_port:客户端 port
$remote_user:使用用户认证时客户端用户输入的用户名
$request_filename:用户请求中的 URI 经过本地 root  或 alias 转换后映射的本地的文件路径
$request_method:请求方法
$server_addr:服务器地址
$server_name:服务器名称
$server_port:服务器端口
$server_protocol:服务器向客户端发送响应时的协议,如 http/1.1,http/1.0
$scheme:在请求中使用的 scheme,如 https://www.magedu.com/ 中的 https
$http_name:匹配请求报文中的指定 HEADER,如 $http_host 匹配请求报文中的 host 首部
$sent_http_name:匹配响应报文中指定的 HEADER,例如 $sent_content_type 匹配响应报文中的 content-type 首部
$status:响应状态

QQ群:397745473

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vsyour

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值