当前位置:首页 > 教程 > 正文内容

nginx!(nginx报413)

2023-05-11 09:30:04教程1

nginx!

在 ngx_lua 中访问 NginX 内置变量 ngx.var.arg_PARAMETER 即可获得GET参数PARAMETER的内容。 如何获取POST请求体数据?

要获得完整的POST请求体数据,可以访问 NginX 内置变量 ngx.var.request_body(注意:由于 NginX 默认在处理请求前不自动读取 request body,所以目前必须显式借助 form-input-nginx 模块才能从该变量得到请求体,否则该变量内容始终为空!)。

如果想获取 POST 方式提交的表单参数,还可以借助 form-input-nginx 模块省去解析过程

nginx报413

在使用uwsgi+nginx部署了django项目之后,原本的上传功能却“失效”了,怎么也上传不了文件,打开chrome控制栏,发现报了如下错误413 Request Entity Too Large,意思大概就是代表请求包太大,服务器拒绝响应。经过查询才知道原来nginx1.3之后默认允许最大请求是2m,nginx1.3之前是1m。

解决办法:

在nginx的配置文件nginx.conf中,添加这么一句client_max_body_size 80m; 意思是最大请求是80m。这个配置可以放到 http段 或者 server段 或者 location段。最后重启nginx,问题就搞定了!

nginxcom

获取url参数

在 ngx_lua 中访问 Nginx 内置变量 ngx.var.arg_PARAMETER 即可获得GET参数PARAMETER的内容。

在 nginx配置中,通过$arg_PARAMETER 即可获得GET参数PARAMETER的内容。

获取请求头

在 ngx_lua 中访问 Nginx 内置变量 ngx.var.http_HEADER 即可获得请求头HEADER的内容。

在 nginx配置中,通过$http_HEADER 即可获得请求头HEADER的内容。

通过以下方式进行验证,比如说,通过 http://www.test.com?name=hello&id=123 来验证url的请求参数,能够在nginx中获取到,只需要修改nginx.conf 配置文件如下,就可以在access.log中看到id和name在log中

http {

include mime.types;

default_type application/octet-stream;

log_format main '{ "@timestamp": "$time_iso8601", '

'"servername": "$http_host", '

'"id": "$arg_id",'

'"name": "$arg_name",'

'"remote_addr": "$remote_addr",'

'"referer": "$http_referer",'

'"request": "$request",'

'"request_time": "$request_time",'

'"status": $status,'

'"bytes":$body_bytes_sent,'

'"agent": "$http_user_agent",'

'"x_forwarded": "$http_x_forwarded_for",'

'"upstr_addr": "$upstream_addr",'

'"upstr_host": "$upstream_http_host",'

'"ups_resp_time": "$upstream_response_time" }';

access_log logs/access.log main;

server_names_hash_bucket_size 128;

nginx常见报错

一、由于启动用户和nginx工作用户不一致所致

1.1查看nginx的启动用户,发现是nobody,而为是用root启动的

命令:ps aux | grep "nginx: worker process" | awk'{print $1}'

1.2将nginx.config的user改为和启动用户一致,

命令:vi conf/nginx.conf

二、缺少index.html或者index.php文件,就是配置文件中index index.html index.htm这行中的指定的文件。

1. server {

2. listen 80;

3. server_name localhost;

4. index index.php index.html;

5. root /data/www/;

6. }

如果在/data/www/下面没有index.php,index.html的时候,直接文件,会报403 forbidden。

三、权限问题,如果nginx没有web目录的操作权限,也会出现403错误。

解决办法:修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重启Nginx即可解决

1. chmod -R 777 /data

2. chmod -R 777 /data/www/

四、SELinux设置为开启状态(enabled)的原因。

4.1、查看当前selinux的状态。

1. /usr/sbin/sestatus

4.2、将SELINUX=enforcing 修改为 SELINUX=disabled 状态。

1. vi /etc/selinux/config

2.

3. #SELINUX=enforcing

4. SELINUX=disabled

4.3、重启生效。reboot。

1. reboot

重启php以及nginx

killall php-fpm && php-fpm &

nginx -s reload

nginx1

https配置步骤:

1、申请SSL证书;

2、在Nginx服务器上面安装SSL证书:

(1)将证书文件(.crt文件)放到指定目录下,例如:/usr/local/nginx/conf/cert

(2)将私钥文件(.key文件)放到指定目录,例如/usr/local/nginx/conf/ssl

(3)若为CA机构签发证书,需要将中间证书文件放到指定目录下,例如/usr/local/nginx/conf/certs

3、修改Nginx配置文件:

(1)打开/etc/nginx/nginx.conf文件

(2)在http部分增加如下配置

ssl on;

ssl_certificate /usr/local/nginx/conf/cert/xxx.crt;

ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.key;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 5m;

(3)如果是CA证书,还需要增加配置

ssl_client_certificate /usr/local/nginx/conf/certs/xxx.crt;

4、重新加载Nginx配置文件:nginx -s reload

5、测试配置是否正确:

(1)curl -k https://www.xxx.com (测试https是否正常)

(2)openssl s_client -connect www.xxx.com:443 (检查ssl证书是否安装正确)

nginxs

是FLEXEM。繁易电子的。网站自己去搜吧~

nginx总结

日志对于统计排错来说非常有利的。本文总结了nginx日志相关的配置如access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。

nginx有一个非常灵活的日志记录模式。每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令来定义。ngx_http_log_module是用来定义请求日志格式的。

1. access_log指令

语法: access_log path [format [buffer=size [flush=time]]];

access_log path format gzip[=level] [buffer=size] [flush=time];

access_log syslog:server=address[,parameter=value] [format];

access_log off;

默认值: access_log logs/access.log combined;

配置段: http, server, location, if in location, limit_except

gzip压缩等级。

buffer设置内存缓存区大小。

flush保存在缓存区中的最长时间。

不记录日志:access_log off;

使用默认combined格式记录日志:access_log logs/access.log 或 access_log logs/access.log combined;

2. log_format指令

语法: log_format name string …;

默认值: log_format combined “…”;

配置段: http

name表示格式名称,string表示等义的格式。log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式,如下所示:

log_format combined '$remote_addr - $remote_user [$time_local] '

' "$request" $status $body_bytes_sent '

' "$http_referer" "$http_user_agent" ';

log_formatcombined'$remote_addr - $remote_user [$time_local] '

' "$request" $status $body_bytes_sent '

' "$http_referer" "$http_user_agent" ';

如果nginx位于负载均衡器,squid,nginx反向代理之后,web服务器无法直接获取到客户端真实的IP地址了。 $remote_addr获取反向代理的IP地址。反向代理服务器在转发请求的http头信息中,可以增加X-Forwarded-For信息,用来记录 客户端IP地址和客户端请求的服务器地址。PS: 获取用户真实IP 参见http://www.ttlsa.com/html/2235.html如下所示:

log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] '

' "$request" $status $body_bytes_sent '

' "$http_referer" "$http_user_agent" ';

log_formatporxy'$http_x_forwarded_for - $remote_user [$time_local] '

' "$request" $status $body_bytes_sent '

' "$http_referer" "$http_user_agent" ';

日志格式允许包含的变量注释如下:

$remote_addr, $http_x_forwarded_for 记录客户端IP地址

$remote_user 记录客户端用户名称

$request 记录请求的URL和HTTP协议

$status 记录请求状态

$body_bytes_sent 发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。

$bytes_sent 发送给客户端的总字节数。

$connection 连接的序列号。

$connection_requests 当前通过一个连接获得的请求数量。

$msec 日志写入时间。单位为秒,精度是毫秒。

$pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。

$http_referer 记录从哪个页面链接访问过来的

$http_user_agent 记录客户端浏览器相关信息

$request_length 请求的长度(包括请求行,请求头和请求正文)。

$request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。

$time_iso8601 ISO8601标准格式下的本地时间。

$time_local 通用日志格式下的本地时间。

$remote_addr,$http_x_forwarded_for记录客户端IP地址

$remote_user记录客户端用户名称

$request记录请求的URL和HTTP协议

$status记录请求状态

$body_bytes_sent发送给客户端的字节数,不包括响应头的大小;该变量与Apache模块mod_log_config里的“%B”参数兼容。

$bytes_sent发送给客户端的总字节数。

$connection连接的序列号。

$connection_requests当前通过一个连接获得的请求数量。

$msec日志写入时间。单位为秒,精度是毫秒。

$pipe如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。

$http_referer记录从哪个页面链接访问过来的

$http_user_agent记录客户端浏览器相关信息

$request_length请求的长度(包括请求行,请求头和请求正文)。

$request_time请求处理时间,单位为秒,精度毫秒;从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。

$time_iso8601ISO8601标准格式下的本地时间。

$time_local通用日志格式下的本地时间。

[warning]发送给客户端的响应头拥有“sent_http_”前缀。 比如$sent_http_content_range。[/warning]

实例如下:

http {

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'"$status" $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for" '

'"$gzip_ratio" $request_time $bytes_sent $request_length';

log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '

'"$status" $body_bytes_sent $request_time $bytes_sent $request_length '

'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

open_log_file_cache max=1000 inactive=60s;

server {

server_name ~^(www\.)?(.+)$;

access_log logs/$2-access.log main;

error_log logs/$2-error.log;

location /srcache {

access_log logs/access-srcache.log srcache_log;

}

}

}

http{

log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '

'"$status" $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for" '

'"$gzip_ratio" $request_time $bytes_sent $request_length';

log_formatsrcache_log'$remote_addr - $remote_user [$time_local] "$request" '

'"$status" $body_bytes_sent $request_time $bytes_sent $request_length '

'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

open_log_file_cachemax=1000inactive=60s;

server{

server_name~^(www\.)?(.+)$;

access_loglogs/$2-access.logmain;

error_loglogs/$2-error.log;

location/srcache{

access_loglogs/access-srcache.logsrcache_log;

}

}

}

3. open_log_file_cache指令

语法: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

open_log_file_cache off;

默认值: open_log_file_cache off;

配置段: http, server, location

对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off),格式如下:

参数注释如下:

max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。

inactive:设置存活时间,默认是10s

min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次

valid:设置检查频率,默认60s

off:禁用缓存

实例如下:

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

1

open_log_file_cachemax=1000inactive=20svalid=1mmin_uses=2;

4. log_not_found指令

语法: log_not_found on | off;

默认值: log_not_found on;

配置段: http, server, location

是否在error_log中记录不存在的错误。默认是。

5. log_subrequest指令

语法: log_subrequest on | off;

默认值: log_subrequest off;

配置段: http, server, location

是否在access_log中记录子请求的访问日志。默认不记录。

6. rewrite_log指令

由ngx_http_rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启。 Nginx重写规则指南

语法: rewrite_log on | off;

默认值: rewrite_log off;

配置段: http, server, location, if

启用时将在error log中记录notice级别的重写日志。

7. error_log指令

语法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];

默认值: error_log logs/error.log error;

配置段: main, http, server, location

配置错误日志。

nginx cgi

CGI的英文全称为Common Gateway Interface(公共网关接口),是Nginx和动态脚本程序的桥梁,Nginx通过FastCGI接口将动态请求发送给FastCGI,FastCGI中的Wrapper进程生成一个线程,将请求交给脚本解释程序执行,然后通过原来的socket将解释执行后的结果原路返回给Nginx,之后Nginx将结果交给客户端。 Nginx是通过套接字文件socket来将动态请求发送给wrapper,使用的是Tcp协议。wrapper通过CGI接口来接受请求。这样web服务器和解释程序之间完全可以独立开发,这样避免了解释程序直接调用服务器的接口而导致的出错和崩溃以及安全性问题。而且可以使Nginx专心处理静态页面请求和转发动态请求,而将脚本解释器安装在另一台服务器,使服务器的压力得到分摊。 CGI是作为PHP程序的补丁开发的,安装PHP首先安装其所依赖的库,之后在编译配置参数时加入对CGI的支持--enable-fpm --enable-cgi等选项。编译PHP的扩展模块,需要用到php中的phpize工具用来生成模块编译时的configure文件,如果运行phpize时无法生成configure文件,原因有:没有安装autoconf软件包。 php-fpm进程的配置文件为/usr/local/php/etc/php-fpm.conf 可以对php-fpm进行相应的配置。 Nginx配置支持fastcgi: location ~ \.php${ root html; fastcgi_pass unix:/tmp/fastcgi.soke //通过套接字文件和cgi建立联系,该文件在php-fpm.conf中设置 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME html$SCRIPT_FILE_NAME;设置参数 include fastcgi_params; //导入fastcgi参数配置文件,该文件在nginx安装时自动生成。 }

nginxlog

Kafka是由Linkedin公司开发,是一个分布式、分区的、多副本的、多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/nginx日志、访问日志,消息服务等等,Linkedin于2010年贡献给了Apache基金会并成为顶级开源项目。

主要应用场景是:日志收集系统和消息系统。

Kafka主要设计目标如下:

以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能保证常数时间的访问性能。

高吞吐率。即使在非常廉价的商用机器上也能做到单机支持每秒100K条消息的传输。

支持Kafka Server间的消息分区,及分布式消费,同时保证每个partition内的消息顺序传输。

同时支持离线数据处理和实时数据处理。

Scale out:支持在线水平扩展

本网站文章仅供交流学习 ,不作为商用, 版权归属原作者,部分文章推送时未能及时与原作者取得联系,若来源标注错误或侵犯到您的权益烦请告知,我们将立即删除.

本文链接:https://www.xibujisuan.cn/98832937.html