nginx 读 章亦春 的博客 的笔记

本文介绍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
[code="java"]# ./configure --prefix=/usr/local/nginx/ --add-module=/root/nginx_test/module/agentzh-echo-nginx-module-9259898
[/code]

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
★★★★★★★★★★★★★
内部跳转
[color=red] echo_exec [/color]

location /foo {
set $a hello;
echo_exec /bar;
}

location /bar {
echo "a = [$a]";
}

结果
[code="java"]
# curl localhost/foo
a = [hello]
#
[/code]
[color=red]类似内联,可通过这种方式传递变量[/color]
★★★★★★★★★★★★★★★
另一种写法[color=red] rewrite [/color]

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

[color=red]注意这俩是只读 的[/color]
结果
[code="java"]# curl 'http://localhost/test?a=b'
uri = /test
request_uri = /test?a=b
# [/code]

★★★★★★
$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为

[code="java"]# 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;
# }
#}

}
#
[/code]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值