nginx相关

来源:互联网 发布:rs485数据采集系统 编辑:程序博客网 时间:2024/05/19 23:00
nginx好处:
第一:IO多路复用模型epoll
http://blog.csdn.net/mango_song/article/details/42643971
第二:轻量级,功能模块少,代码模块化
第三:cpu亲和,是一种把cpu核心和nginx工作进程绑定方式,把每个worker进程固定在一个cpu上执行,减少切换cpu
第四:sendfile系统调用是一种文件传输的系统调用“零拷贝(zero-copy)”


检查配置文件是否正确

nginx -t -c /etc/nginx/nginx.conf

基础
user  root root; 


worker_processes 8; 
error_log /usr/local/nginx/logs/nginx_error.log crit;##错误日志输出路径


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


events 

  use epoll; 
  worker_connections 65535; 

server{
listen       80;##监听端口

server_name t.meidaojia.com;##作为代理服务的访问服务名

location / { ##访问跟路径的时候,看到的页面是服务器目录/usr/share/ningx/html下的页面

root /usr/share/ningx/html

#random_index on;########################返回随机主页面,默认是关闭的,现在配置为打开即可

index index.html index.htm

}

error_page 500 502 503 504 404 /50x.html;##访问跟路径的时候,如果访问50x页面,看到的页面是服务器目录/usr/share/ningx/html下的页面50x.html

location = /50x.html{

root /usr/share/nginx/html

}

access_log /usr/local/nginx/logs/access_mdj_v02.log access_mdj_v02;########access_mdj_v02格式的定义在http模块下的log_format定义的
}

http{ 

log_format  access_mdj_v02  'V02{server_addr:"$server_addr",http_host:"$http_host",remote_addr:"$remote_addr",remote_port:"$remote_port",remote_user:"$remote_user",time_local:"$time_local",time_iso8601:"$time_iso8601",msec:"$msec",request:"$request",request_method:"$request_method",request_uri:"$request_uri",query_string:"$query_string",status:$status,content_type:"$sent_http_content_type",body_bytes_sent:$body_bytes_sent,http_referer:"$http_referer",http_user_agent:"$http_user_agent",cookie:"$http_Cookie",request_time:$request_time,cus_uid:"$cookie_sid$cookie_JSESSIONID",connection:"$connection",connection_requests:"$connection_requests","uri":"$uri"}';


  ###20160922#####
    include /usr/local/nginx/conf/conf.d/server/*.conf;
    include /usr/local/nginx/conf/conf.d/upstream/*.conf;
}





nginx变量,http请求变量arg_参数、http_HEADER、send_http_HEADER(服务端返回给客户端的头)

remote_addr客户端的地址

remote_user认证的用户(如果没有配置,则没有)

http_referer前一次的地址url

http_user_agent客户端,比如IE,Chrome

http_x_forwarded_for 记录所有的客户端来源的地址,优于remote_addr


模块

http_stub_status_module配置

location /mystatus{

stub_status;

}

说明:访问该路径,会返回nginx当前的一些状态信息,连接数,请求数,握手次数,读写数等


random_index_module配置

location / {

root /usr/share/ningx/html

random_index on;########################返回随机主页面,默认是关闭的,现在配置为打开即可,前提是该目录下有多个随机页面

}


http_sub_module配置

sub_filter str(要替换的)   replacement(替换后的)

sub_filter_last_modified on;默认是关闭的(如果服务端返回有改变,则返回最新的)

sub_filter_once on;全局,(是全部匹配,还是只匹配第一个,比如替换全局还是第一个)


请求限制

定义

limit_req_zone key(比如客户端ip)  zone=name:size rate=rate;

使用

limit_req zone=name burst=3(超出的,只遗留三个,在下一秒执行) nodelay(不延迟,直接拒绝);


连接限制(先连接,后请求)

定义

limit_conn_zone key(比如客户端ip)  zone=name:size;

使用

limit_conn name 1(同一时刻,只允许一个连接);


访问控制

示例

location ~^ /admin.html {

root /opt/app/code;

deny 222.128.189.17/100;#######禁止访问的ip段

allow all;#####其他都允许访问

index index.html index.htm

}


进行身份验证,有一定的局限性,最好使用nginx+lua

location ~^ /admin.html {

root /opt/app/code;

auth_basic "Auth access test!put your password"

auth_basic_user_file /etc/nginx/auth_conf

index index.html index.htm

}



知识点

http1.0  tcp不能复用

http1.1 顺序性tcp复用

http2.0 多路复用tcp复用




原创粉丝点击