参考源:

github: https://aceld.gitbooks.io/nginx-zh/content/22_zheng_shi_an_zhuang.html

书籍:《nginx高性能 web服务器详解》


测试工具

客户端----postman: https://twitter.com/postmanclient

linux:centos7

服务端: netty 编写小程序 github路径: https://github.com/undergrowthlinear/MyCodeLearn/tree/master/component/netty-mw/src/test/java/mycodelearn/undergrowth/netty/http



用processon画的 思维图:




对应的nginx.cnf文件 必要地方加了注释

#使用nging的用户与组
user  nobody;

#worker进程数,与cpu数目一致
worker_processes  2;

#错误日志存放目录
error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#nginx进程id--netstat -aonp | grep nginx
pid        logs/nginx.pid;

#事件配置块
events {
   #使用epoll事件驱动
   use epoll;

   #worker连接数
    worker_connections  1024;
}


#http配置块
http {
    #引入支持的content-type
    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;

    #zlib支持的gzip
    gzip  on;
    #多大数据 开始压缩 1k
    gzip_min_length 1024;
   #压缩级别1--9 1压缩最低 9压缩最高
   gzip_comp_level 2;
   #开启gzip压缩头
   gzip_vary on;
   #loadbalance

   #配置后端服务器组
   upstream modelServer.com {
	server 192.168.126.129:8091;
	server 192.168.126.129:8092;
	server 192.168.126.129:8093;
	}
	
   upstream 8843server.com {
	server 192.168.1.104:8844;
	server 192.168.1.104:8845;
	server 192.168.1.104:8846;
	}

    #虚拟主机配置
    server {
	#监听端口
	listen	8843;
	#监听网卡----可使用 ifconfig ech0:0 192.168.126.130 netmask 255.255.255.0 up 
	#为同一网卡添加多个ip
	server_name	localhost;
	
	#虚拟主机访问日志存放路径与格式
	access_log	logs/8843.access.log main;

	#最后匹配
	location	/ {
	#反向代理为后端服务器组
	proxy_pass	http://8843server.com;
	proxy_pass_request_headers on;
	}

	#alias正则匹配
	#转换根路径
	location ~ ^/data/(.+\.html)$ {
	alias alias/$1;
	}

	#通过rewrite实现uri重写
	#将/data开头,.bd结尾的uri 永久转向为http://www.baidu.com访问
	location ~ ^/data/(.+\.bd)$ {
	#对参数进行判断
	#参数有goog则转向google
	if ($args ~ web=goog*){
	rewrite ^(.*) http://www.google.com permanent;
	}
	#没有参数 则转向百度
	rewrite ^(.*) http://www.baidu.com permanent;
	}

	#正则匹配 
	location ~ \.do$ {
	#允许ip
	allow 192.168.126.1;
	#禁用所有ip访问
	deny all;
	}

	#错误页面
	error_page 404 /notdound.html;
	#错误页面重定向为后端服务器组
	location = /notfound.html {
	proxy_pass http://8843server.com;
	#开启本地磁盘缓存响应数据
	proxy_store on;
	proxy_store_access user:rw group:rw all:r;
	proxy_temp_path storetmp/ ;
	}
	
	}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
	    proxy_pass http://modelServer.com;
	 proxy_pass_header host; 
           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;
        #}
    }

    server {
	listen	8777;
	server_name	localhost;
	location / {
	#自定义模块测试
		mytest;
#		root	html;
#		index	index.html index.htm;
	}
	}	

    # 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 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}



对于自定义模块测试 源码参看

github: https://aceld.gitbooks.io/nginx-zh/content/7_zi_ding_yi_nginx_mo_kuai.html

或者nginx高性能web 的16章




简单介绍几个常用的配置

1、rewrite

postman


nging log


nginx.conf





2、自定义模块






Logo

更多推荐