1、 YUM安装
yum安装很简单,但是安装的版本比较旧,要使用最新版本的nginx可以使用编译安装
yum install nginx -y
2、 编译安装
2.1 安装依赖包
yum -y install gcc gcc-c++ automake autoconf libtool make
- 安装pcre,zlib,pcre为了重写rewrite,zlib为了gzip压缩
进入源码目录,可以是任何目录,本文选定的是/usr/local/src 安装pcre
cd /usr/local/src wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.40/pcre2-10.40.tar.gz tar -zxvf pcre2-10.40.tar.gz cd pcre2-10.40/ ./configure make&&make install
安装zlib库
cd /usr/local/src wget http://zlib.net/zlib-1.2.12.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make&&make install
安装openssl
cd /usr/local/src wget https://www.openssl.org/source/openssl-1.1.1q.tar.gz tar -zxvf openssl-1.1.1l.tar.gz
2.2 安装nginx
cd /usr/local/src wget https://nginx.org/download/nginx-1.22.0.tar.gz tar -zxvf nginx-1.21.1.tar.gz cd nginx-1.21.1 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gunzip_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre2-10.40 --with-zlib=/usr/local/src/zlib-1.2.12 --with-openssl=/usr/local/src/openssl-1.1.1q --without-http_autoindex_module make make install
2.3 启动Nginx
创建用户:
useradd nginx -s /sbin/nologin
cd /usr/local/src/nginx sbin/nginx
查看Nginx是否启动成功
ps -ef |grep nginx root 29108 0.0 0.0 33240 756 ? Ss 22:07 0:00 nginx: master process sbin/nginx nginx 29109 0.0 0.2 67992 4448 ? S 22:07 0:00 nginx: worker process root 29111 0.0 0.0 9208 1092 pts/0 S+ 22:07 0:00 grep --color=auto nginx
查看端口
netstat -nltp |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32413/nginx: master
2.4 防火墙中添加80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功
2.5 添加Nginx软连接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
2.6 加入系统服务
vim /lib/systemd/system/nginx.service [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
2.7 检测配置参数是否正确
sbin/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
2.8 配置多站点
进入nginx安装目录,修改配置文件
cd /usr/local/nginx/conf
vim nginx.conf
在http{}模块内,添加include配置信息
include /etc/nginx/conf.d/*.conf; 表示Nginx将会从该路径下的所有.conf文件获取站点信息
在 /etc/nginx/conf.d 目录下新建conf文件,有几个站点就新建几个conf文件
3、性能优化
Worker进程数量
Nginx运行工作进程个数一般设置为CPU的核心数或者核心数x2。如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件grep processor /proc/cpuinfo | wc -l
绑定nginx work进程和CPU核心
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;worker_processes 2;
worker_cpu_affinity 0101 1010;worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_processes最多开启8个,8个以上性能不会再提升了,而且稳定性变得更低,所以8个进程够用了。在nginx 1.9版本之后,就帮我们自动绑定了cpu
worker_processes auto;
设置work_connections 连接数
worker_connections 10240
每个进程的最大文件打开数,等于ulimit -n 的系统值
worker_rlimit_nofile 65536
Linux系统最大的文件打开数
ulimit -n 65536
追加到 /etc/security/limits.conf,然后执行sysctl -p 生效
ulimit -a #查看所有属性值keepalive timeout会话保持时间
keepalive_timeout 60
GZIP压缩性能优化
gzip on
gzip_min_length 1k; #表示允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,表示不管页面多大都进行压缩,建议设置成大于1K。如果小于1K可能会越压越大
gzip_buffers 4 32k; #压缩缓存区大小
gzip_http_version 1.1; #压缩版本
gzip_comp_level 6; #压缩比率, 一般选择4-6,压缩级别越大,就越消耗CPU
gzip_types text/css text/xml application/javascript application/json; #指定压缩的类型,图片、音视频不要压缩
gzip_vary on; #支持前端缓存服务器存储压缩页面proxy超时设置
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k高效传输模式
sendfile on; # 开启高效文件传输模式。
tcp_nopush on; #需要sendfile在开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量。将响应头和正文的开始部分一起发送,而不一个接一个的发送。重定向HTTP请求到HTTPS
server { listen 80; server_name localhost; charset utf-8; return 301 https://$server_name$request_uri; }
评论