mofdan 发表于 2016-12-27 07:42:51

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
# ./configure --prefix=/usr/local/nginx/ --add-module=/root/nginx_test/module/agentzh-echo-nginx-module-9259898


nginx.conf

最上面
userroot;
否则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]";
}

结果

# curl localhost/foo
a =
#

类似内联,可通过这种方式传递变量
★★★★★★★★★★★★★★★
另一种写法 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";
}

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

★★★★★★
$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为
# cat nginx.conf
userroot;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pid      logs/nginx.pid;

events {
worker_connections1024;
}

http {
include       mime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfile      on;
#tcp_nopush   on;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
########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_namelocalhost;
#charset koi8-r;
#access_loglogs/host.access.logmain;
###########################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;
indexindex.html index.htm;
}
#error_page404            /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_indexindex.php;
#    fastcgi_paramSCRIPT_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 {
#    denyall;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_namesomenamealiasanother.alias;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443;
#    server_namelocalhost;
#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_keycert.key;
#    ssl_session_timeout5m;
#    ssl_protocolsSSLv2 SSLv3 TLSv1;
#    ssl_ciphersHIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers   on;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}
}
#
页: [1]
查看完整版本: nginx 读 章亦春 的博客 的笔记