nginx 读 章亦春 的博客 的笔记

打开目录浏览功能
http://wenku.baidu.com/view/1acdff2b453610661ed9f4f0.html###
打开nginx.conf文件,在location server 或 http段中加入
autoindex on;
就可以访问目录结构了

★★★★★★★★★★★★
读nginx笔记http://blog.sina.com.cn/s/blog_6d579ff40100wi7p.html
环境fedora17

yum install zlib
yum install pcre-devel

解压nginx和echo-nginx-module
下载地址
https://github.com/agentzh/echo-nginx-module/tags
[root@leaflinux nginx-1.3.4]# ./configure --prefix=/usr/local/nginx/ --add-module=/root/nginx_test/module/agentzh-echo-nginx-module-9259898


nginx.conf
最上面
user  root;
否则403
.....server的上面添加
    geo $dollar {
        default "$";
    }
    
    server {
..........
     添加
        location /test {
                echo "This is a dollar sign: $dollar";
        }


./nginx
curl http://localhost/test
输出
This is a dollar sign: $
★★★★★★★★★★★★★★★★★★
location /test1 {
            set $first "hello ";
            echo "${first}world";
        }


curl localhost/test1
hello world
★★★★★★★★★★★★★
内部跳转
echo_exec
location /foo {
            set $a hello;
            echo_exec /bar;
        }
 
        location /bar {
            echo "a = [$a]";
        }

结果
[root@leaflinux objs]# curl localhost/foo
a = [hello]
[root@leaflinux objs]#

类似内联,可通过这种方式传递变量
★★★★★★★★★★★★★★★
另一种写法 rewrite
  location /foo {
            set $a hello;
            rewrite ^ /bar;
        }
 
        location /bar {
            echo "a = [$a]";
        }

rewrite 还能进行301和302的外部跳转

★★★★★★★★★★★★★★
location /test {
       echo "uri = $uri";
       echo "request_uri = $request_uri";
}

注意这俩是只读 的
结果
[root@leaflinux objs]# curl 'http://localhost/test?a=b'
uri = /test
request_uri = /test?a=b
[root@leaflinux objs]# 


★★★★★★
$arg_XXX 变量群。
 location /test {
        echo "name: $arg_name";
        echo "class: $arg_class";
    }

结果
$ curl 'http://localhost:8080/test'
    name: 
    class: 
 
    $ curl 'http://localhost:8080/test?name=Tom&class=3'
    name: Tom
    class: 3

转义可以用
 location /test {
        set_unescape_uri $name $arg_name;
        set_unescape_uri $class $arg_class;
 
        echo "name: $name";
        echo "class: $class";
    }


★★★★★★★★★★★
以下为Nginx 变量漫谈(三)
 
server {
        listen 8080;
 
        location /test {
            set $args "foo=1&bar=2";
            proxy_pass http://127.0.0.1:8081/args;
        }
    }
 
    server {
        listen 8081;
 
        location /args {
            echo "args: $args";
        }
    }

输出
 $ curl 'http://localhost:8080/test?blah=7'
    args: foo=1&bar=2


综上

整个nginx.conf为

[root@leaflinux conf]# cat nginx.conf

user  root;
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;
    ########haoning#########begin
        geo $dollar {
        default "$";
    }
    server {
        listen 8080;
 
        location /test {
            set $args "foo=1&bar=2";
            proxy_pass http://127.0.0.1:8081/args;
        }
    }
 
    server {
        listen 8081;
 
        location /args {
            echo "args: $args";
        }
    } 
    ########haoning#########end
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
###########################haoning#################
                autoindex on;
     #0.
     #  location /test {
     #        echo "This is a dollar sign: $dollar";
     #  }
     #  location /test1 {
     #        set $first "hello ";
     #        echo "${first}world";
     #  }
         #1.
         #      location /bad {
     #        echo $foo;
     #   }
     #2.
     #  location /foo {
     #        set $a hello;
     #        echo_exec /bar;
     #  }
     #  location /bar {
     #        echo "a = [$a]";
     #  } 
     #3.
         #  location /foo {
         #      set $a hello;
         #      rewrite ^ /bar;
         #  }
         #
         #  location /bar {
         #      echo "a ===== [$a]";
         #  }
         #4.
        #       location /test {
        #                       echo "uri = $uri";
        #                       echo "request_uri = $request_uri";
        #       }
                location /test {
                                echo "name: $arg_name";
                                echo "class: $arg_class";
                }

###########################haoning#################





        location / {
            root   html;
            index  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           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # 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;
    #    }
    #}

}
[root@leaflinux conf]# 

猜你喜欢

转载自haoningabc.iteye.com/blog/1621661
今日推荐