Nginx 的编译安装和配置
Yum 安装 直接执行:
yum -y install nginx
这里我们主要是编译安装:
首先安装编译nginx所需的大部分依赖工具:
yum -y install gcc gcc-c++ make automake autoconf pcre pcre-devel zlib zlib-devel openssl openssl-devel libtool
创建nginx用户:
useradd -s /sbin/nologin -M nginx
编译安装:
tar -zxvf nginx-1.16.1.tar.gz #解压安装包
cd nginx-1.16.1 #进入目录
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module
等待完成后:
make && make install
完成后我们可以在/usr/local/nginx下看到已经安装的nginx
nginx.conf 优化配置:
#user nobody;
worker_processes 4;
#worker_cpu_affinity 01 10;
#worker_cpu_affinity 0001 0010 0100 1000;
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
#worker_rlimit_nofile 65535;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
events {
worker_connections 20480;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on; #开启高效传输 防止IO阻塞
tcp_nopush on; #减少网络报文数量
tcp_nodelay on; #提高IO性能
keepalive_timeout 6000;
client_header_timeout 1000;
client_body_timeout 6000;
reset_timedout_connection on;
send_timeout 6000;
charset UTF-8;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 6; # 压缩比率 9最大 消耗CPU
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
client_header_buffer_size 1024k;
large_client_header_buffers 4 1024k;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
include extras/*.conf;
}
Vhosts配置:
server {
listen 80;
server_name cqjd.yhjyzx.com;
rewrite ^/(.*) https://$server_name$request_uri? permanent; #http跳转https
}
静态文件正则加载:
location ~ ^/(plugins|js|styles)/ {
root /usr/local/nginx/html/staticfile/;
autoindex on;
expires 30d; #降低网站带宽 加快访问速度
}
location块配置:
#禁止浏览器直接访问
# internal;
limit_rate 2048k;

浙公网安备 33010602011771号