Nginx常用的配置:
主配置文件 nginx.conf
vim /usr/local/nginx/conf
user work work;
# 设置所有worker进程可以打开的最大文件句柄数
worker_rlimit_nofile 65535;
worker_processes auto;
worker_cpu_affinity auto;
events {
# 让多个worker进程轮询相应新请求
accept_mutex on;
# 限制每个worker进程的并发连接数
worker_connections 10240;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
# 开启高效文件传输模式
sendfile on;
tcp_nodelay on;
tcp_nopush on;
# 设置客户端连接保持会话的超时时间
keepalive_timeout 10;
# 设置客户端请求头读取超时时间
client_header_timeout 15;
# 设置客户端请求主体读取超时时间
client_body_timeout 15;
# 指定响应客户端的超时时间
send_timeout 15;
# 设置最大的容许的客户端请求主体大小
client_max_body_size 10M;
# 将http请求响应头里的nginx版本号信息隐藏
server_tokens off;
# 开启压缩
gzip on;
# 设置容许压缩的页面最小字节数
gzip_min_length 1k;
# 申请4个单位为32K的内存作为压缩结果流缓存
gzip_buffers 4 32k;
# 压缩的版本
gzip_http_version 1.1;
# 由于IE6对Gzip压缩效果不好,建议在IE6的环境下关闭压缩功能
gzip_disable "MSIE[1-6]";
# 压缩比率
gzip_comp_level 3;
# 指定压缩的类型
gzip_types text/plain text/css text/xml application/javascript application/x-javascript;
# 设置从web服务器传给缓存服务器的时候不被解压。只有传送到用户后才解压缩
gzip_vary on;
server_tokens off;
access_log off;
#加载扩展目录下的所有配置
include conf.d/*.conf ;
}扩展目录下的文件配置
cd conf.d/ vim www.conf
server {
#监听端口
listen 80;
server_name www.xxx.cn;
#应用程序目录
root /home/work/www;
#获取客户端真实IP(需安装realip模块,方法在下边)
set_real_ip_from 10.0.0.0/16;
set_real_ip_from 192.168.0.0/16;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
#错误日志
error_log /usr/local/nginx/logs/nginx_error.log crit;
location / {
index index.html;
}
# 设置资源缓存时间
location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
# 设置资源缓存时间
location ~ .*\\.(js|css)?$ {
expires 1d;
}
# 设置状态页访问信息
location /ngx_status {
stub_status on;
access_log off;
allow 192.168.0.0/16;
deny all;
}
access_log /usr/local/nginx/logs/access.log;
}set_real_ip_from:告诉nginx,192.168.0.0是我们的反向代服务器不是真实的用户IP
real_ip_header:告诉nginx真正的用户IP是存在X-Forwarded-For请求头中
当real_ip_recursive为off时,nginx会把real_ip_header指定的HTTP头中的最后一个IP当成真实IP
当real_ip_recursive为on时,nginx会把real_ip_header指定的HTTP头中的最后一个不是信任服务器的IP当成真实IP。
安装realip模块
./configure --prefix=/usr/local/nginx --user=work --group=work --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
本文地址:https://www.stayed.cn/item/264
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我