编译安装Nginx1.16及配置优化
访问Nginx官网(http://nginx.org/en/download.html)找到您想要的版本进行下载,这里选择的是nginx-1.16.1。
$ wget http://nginx.org/download/nginx-1.16.1.tar.gz
1、安装依赖
$ yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
2、编译安装Nginx
# 解压tar包
$ tar zxf nginx-1.16.1.tar.gz -C /tmp/
# 创建程序用户
$ groupadd nginx && useradd -g nginx nginx -s /sbin/nologin
# 编译安装Nginx
$ cd /tmp/nginx-1.16.1/
$ ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --with-stream --user=nginx --group=nginx && make && make install
# 添加软连接
$ ln -s /usr/local/sbin/nginx /usr/sbin/nginx
3、验证安装
$ nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4、配置优化
点击查看nginx.conf代码
$ cat /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
# -------------------------- logs config ------------------------------------
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'"$http_referer" $status $body_bytes_sent $request_body '
'"$http_user_agent" "$http_x_forwarded_for"';
map $http_x_forwarded_for $clientRealIp {
"" $remote_addr;
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
}
log_format json '{"accessip_list":"$proxy_add_x_forwarded_for",'
'"client_ip":"$clientRealIp",'
'"http_host":"$host",'
'"@timestamp":"$time_iso8601",'
'"method":"$request_method",'
'"url":"$request_uri",'
'"status":"$status",'
'"http_referer":"$http_referer",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"http_user_agent":"$http_user_agent",'
'"total_bytes_sent":"$bytes_sent",'
'"server_ip":"$server_addr"}';
error_log logs/error.log notice;
access_log logs/access.log main;
# ---------------------------------------------------------------------------
sendfile on;
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn perip 10000;
limit_rate 10000k;
keepalive_timeout 300;
tcp_nodelay on;
server_names_hash_bucket_size 512;
client_header_buffer_size 1m;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
large_client_header_buffers 4 512k;
reset_timedout_connection on;
send_timeout 150;
server_tokens off;
client_header_timeout 120s;
client_max_body_size 200m;
client_body_timeout 120s;
client_body_buffer_size 1m;
proxy_request_buffering off;
proxy_buffering off;
proxy_connect_timeout 500s;
proxy_send_timeout 500s;
proxy_read_timeout 500s;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /usr/local/nginx/nginx_tmp;
fastcgi_intercept_errors on;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;
gzip on;
gzip_min_length 2k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
gzip_proxied any;
# 用于配置websocket
map $http_upgrade $connection_upgrade {
default Upgrade;
'' close;
}
# -------------------------- 虚拟主机配置 ----------------------------------
server {
listen 80;
server_name localhost;
charset utf-8;
access_log logs/access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include conf.d/*.conf;
}
5、加入systemctl系统服务
$ vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
$ systemctl daemon-reload # 加载配置
$ systemctl start nginx # 启动
$ systemctl stop nginx # 停止
$ systemctl restart nginx # 重启
$ systemctl status nginx # 查看服务状态
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2021-10-19 09:50:23 CST; 4s ago
Process: 25562 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 25563 (nginx)
Tasks: 7
Memory: 111.1M
CGroup: /system.slice/nginx.service
├─25563 nginx: master process /usr/sbin/nginx
├─25564 nginx: worker process
├─25565 nginx: worker process
├─25566 nginx: worker process
├─25567 nginx: worker process
├─25568 nginx: cache manager process
└─25569 nginx: cache loader process
Oct 19 09:50:23 research-cicd systemd[1]: Starting nginx - high performance web server...
Oct 19 09:50:23 research-cicd systemd[1]: Started nginx - high performance web server.
6、设置开机自启
$ systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

浙公网安备 33010602011771号