nginx 使用总结

nginx 使用总结

nginx 使用总结

几个常见配置项:

  • 1.$remote_addr 与 $http_x_forwarded_for 用以记录客户端的ip地址;
  • 2.$remote_user :用来记录客户端用户名称;
  • 3.$time_local : 用来记录访问时间与时区;
  • 4.$request : 用来记录请求的url与http协议;
  • 5.$status : 用来记录请求状态;成功是200;
  • 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;
  • 7.$http_referer :用来记录从那个页面链接访问过来的;
  • 8.$http_user_agent :记录客户端浏览器的相关信息;


1、location 指令说明
  该指令用于匹配 URL,语法如下:
    1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配 成功,就停止继续向下搜索并立即处理该请求。
    2、~:用于表示 uri 包含正则表达式,并且区分大小写。
    3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。
    4、^~:用于不含正则表达式的uri前,要求Nginx服务器找到标识uri和请求字 符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。

2、根据设备跳转
  if ( $http_user_agent ~* "(Android|iPhone|Windows Phone|UC)" ){
# proxy_pass http://localhost:9019;
rewrite ^/(.*) https://m.huamu0101.com/$1 redirect;
}

if ( $http_user_agent ~ "(MIDP)|(WAP)|(Smartphone)|(Mobile)|(WxdB.Browser)|(UCWEB)|(Mini)|(Symbian)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(SAMSUNG)|(Motorola)|(LG-)|(LG/)|(iPhone)|(Android)|(Wget)" ){
# proxy_pass http://localhost:9019;
rewrite ^/(.*) https://m.huamu0101.com/$1 redirect;
}

3、80跳转443

if ($server_port !~ 443){
  rewrite ^(/.*)$ https://$host$1 permanent;
 }
跳转 www
server {
listen 80;
listen 443 ssl;
server_name xxx.com;

ssl on;
ssl_certificate /usr/local/nginx/cert/www.xxx.com.pem;
ssl_certificate_key /usr/local/nginx/cert/www.xxx.com.key;

rewrite ^(.*)$ https://www.${server_name}$1 permanent;
}
 

  4、日志按照日期切割

  if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
     set $log_date_filename $1;
  }

  5、路径替换

# 路径替换
location /zb_users/ {
rewrite /zb_users/(.*) /image/zb_users/$1 break;
proxy_pass http://xxx.xxx.com;
}
# 路径匹配
location /image/bamboo/ {
# rewrite /image/bamboo/(.*) /image/bamboo/$1 break;
proxy_pass http://xxx.xxx.com;
}

6、静态文件配置
    location ~ / {
        index   index.html;
        root    /usr/local/nginx/html/xxx/;
    }

    location ~ /beian/ {
        root    /usr/local/nginx/html/xxx/;
    }
 
  location = /favicon.ico {
  root /usr/local/nginx/html/xxx/;
  }
location /logapi/v1/get/js/post/data.js {   
alias /usr/local/openresty/nginx/html/online/data.js;
}

7、开启服务
location /lualogshow {
set $log "log";
add_header Lua $log;
alias /usr/local/openresty/nginx/logs;
autoindex on;
#开启以服务器本地时区显示文件修改日期!
autoindex_localtime on;
   # 只预览不下载
if ($request_filename ~* ^.*?\.(txt|log)$){
add_header Content-Type 'text/plain;charset=utf-8'; # 解决目录中文乱码
}
# 只下载不预览
if ($request_filename ~* ^.*?\.(rar|gz|zip|exe)$){
add_header Content-Disposition attachment;
}
}

8、rewrite
location / {
rewrite ^/(.*) http://www.xxx.com/$1 permanent;
}

9、直接返回字符串
  location = /baidu_s.html {
        return 200 "b4d76cc6d17db4d536f22b84e6c839";
    }
 
10、
location ~* ^/peixun/zt-(\d+)/?$ {
access_log off;
rewrite ^/peixun/zt-(\d+)/?$ /peixun/wap-zt-$1/ permanent;
}
 

 

1、arg_PARAMETER HTTP 请求中某个参数的值,如/index.php?site=www.ttlsa.com,可以用arg 
PARAMETERHTTP请求中某个参数的值,如/index.php?site=www.ttlsa.com,可以用arg_site取得www.ttlsa.com这个值。

2、args HTTP 请求中的完整参数。例如,在请求/index.php?width=400&height=200 中,args表示字符串width=400&height=200

3、$binary_remote_addr 二进制格式的客户端ip地址。例如:\x0A\xE0B\x0E

4、$body_bytes_sent 表示在向客户端发送的http响应中,包体部分的字节数

5、$content_length 表示客户端请求头部中的Content-Length 字段

6、$content_type 表示客户端请求头部中的Content-Type 字段

7、$cookie_COOKIE 表示在客户端请求头部中的cookie 字段

8、$document_root 表示当前请求所使用的root 配置项的值

9、$uri 表示当前请求的URI,不带任何参数

10、document_uri 与document 
    uri与uri 含义相同

11、request_uri 表示客户端发来的原始请求URI,带完整的参数。request 
uri表示客户端发来的原始请求URI,带完整的参数。uri和document_uri未必是用户的原始请求,在内部重定向后可能是重定向后的URI,而document 
uri未必是用户的原始请求,在内部重定向后可能是重定向后的URI,而request_uri 永远不会改变,始终是客户端的原始URI

12、host 表示客户端请求头部中的Host字段。如果Host字段不存在,则以实际处理的server(虚拟主机)名称代替。如果Host字段中带有端口,如IP:PORT,那么host表示客户端请求头部中的Host字段。如果Host字段不存在,则以实际处理的server(虚拟主机)名称代替。如果Host字段中带有端口,如IP:PORT,那么host是去掉端口的,它的值为IP。$host 是全小写的。这些特性与http_HEADER中的http_host不同,http_host只取出Host头部对应的值。

13、$hostname 表示 Nginx所在机器的名称,与 gethostbyname调用返回的值相同

14、$http_HEADER 表示当前 HTTP请求中相应头部的值。HEADER名称全小写。例如:请求中 Host头部对应的值 用 $http_host

15、$sent_http_HEADER 表示返回客户端的 HTTP响应中相应头部的值。HEADER名称全小写。例如,用 $sent_ http_content_type表示响应中 Content-Type头部对应的值

16、is_args 表示请求中的 URI是否带参数,如果带参数,is 
args表示请求中的URI是否带参数,如果带参数,is_args值为 ?,如果不带参数,则是空字符串

17、$limit_rate 表示当前连接的限速是多少,0表示无限速

18、$nginx_version 表示当前 Nginx的版本号

19、$query_string 请求 URI中的参数,与 $args相同,然而 $query_string是只读的不会改变

20、$remote_addr 表示客户端的地址

21、$remote_port 表示客户端连接使用的端口

22、$remote_user 表示使用 Auth Basic Module时定义的用户名

23、$request_filename 表示用户请求中的 URI经过 root或 alias转换后的文件路径

24、$request_body 表示 HTTP请求中的包体,该参数只在 proxy_pass或 fastcgi_pass中有意义

25、$request_body_file 表示 HTTP请求中的包体存储的临时文件名

26、$request_completion 当请求已经全部完成时,其值为 “ok”。若没有完成,就要返回客户端,则其值为空字符串;或者在断点续传等情况下使用 HTTP range访问的并不是文件的最后一块,那么其值也是空字符串。

27、$request_method 表示 HTTP请求的方法名,如 GET、PUT、POST等

28、$scheme 表示 HTTP scheme,如在请求  https://nginx.com/中表示 https

29、$server_addr 表示服务器地址

30、$server_name 表示服务器名称

31、$server_port 表示服务器端口

32、$server_protocol 表示服务器向客户端发送响应的协议,如 HTTP/1.1或 HTTP/1.0

 

posted @ 2022-08-19 15:43  小学弟-  阅读(201)  评论(0编辑  收藏  举报