Nginx的安装与配置

弱龄寄事外,委怀在琴书。这篇文章主要讲述Nginx的安装与配置相关的知识,希望能为你提供帮助。
9.1 nginx概述9.1.1 Nginx是什么
Nginx是一个轻量级、高性能的Web服务器和反向代理服务器,同时也是一个比较优秀的负载均衡服务器和缓存服务器,可以运行在LINUX和WINDOWS上。
功能如下:
1、Web服务器:与Apache相比,它能支持的并发连接更多。占用服务器资源较少,并且请求处理效率较高。
2、反向代理服务器:Nginx可以做为HTTP服务器或数据库服务的代理服务器。它的代理功能相对简单,处理请求的效率不及Haproxy。
3、负载均衡服务器:Nginx可以将客户端的请求流量分配给后端多个应用程序服务器,从而提高WEB应用程序的性能、可伸缩性与可靠性。
4、缓存服务器:Nginx可以作为缓存服务器,与专业的缓存服务器功能相似。
9.1.2 Nginx的优点
Nginx的优点如下:
1、高并发:能支持1万--2万甚至更多的并发连接(静态文件小更强)。
2、处理请求对服务器的资源消耗较少。
3、内置对集群节点的健康性能检查功能,但功能相对较弱。
4、可以通过Cache插件实现缓存软件的功能。
9.2 Nginx安装
9.2.1 Nginx安装环境准备
1、安装Nginx依赖软件包
[root@site101 /]# yum install

  • ot@site101 /]# yum install -y pcre-devel make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2、下载Nginx
官网:www.nginx.org ,分为三个版本。开发版(Mainline)、稳定版(Stable version)、历史版本(Legacy version)。
9.2.2 Nginx安装及编译
1、编译和安装过程和参数和过程如下:
[root@site101 nginx-1.20.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.20.2/ --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-pcre
[root@site101 nginx-1.20.2]# make & & make install
[root@site101 nginx-1.20.2]# ln -s /usr/local/nginx-1.20.2/ /usr/local/nginx
2、启动服务并检查
[root@site101 nginx-1.20.2]# /usr/local/nginx/sbin/nginx -t
[root@site101 nginx-1.20.2]# /usr/local/nginx/sbin/nginx
[root@site101 nginx-1.20.2]# lsof -i :80
COMMANDPIDUSERFDTYPE DEVICE SIZE/OFF NODE NAME
nginx37108root6uIPv4628330t0TCP *:http (LISTEN)
nginx37109 nginx6uIPv4628330t0TCP *:http (LISTEN)
9.3、配置nginx支持php
对于apache来说,php是其一个模块,没有独立的进程,配置apache支持php即可。
对于nginx来说,PHP有独立的进程,依靠fastcgi进程来运行,因此支持php的配置与apache不同。
9.3.1 下载安装PHP7.0.0
参见8、mysql与php安装
9.3.2 修改PHP配置文件
1).在执行configure的地方执行如下命令
cp php.ini-production /opt/local/php7/etc/php.ini
cd /opt/local/php7/etc///查看是否有了 php.ini
2).在接着执行如下命令
cp php-fpm.conf.default php-fpm.conf
3).在进入这个目录 cd php-fpm.d/在继续执行如下命令
cp www.conf.default www.conf
9.3.3 配置nginx
1、修改nginx主配置文件,对fastcgi进行优化
[root@site101 conf]# vim nginx.conf
http
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

2、修改虚拟主机配置文件
nginx.conf 中虚拟主机模块 server中修改如下:
location ~ .*\\.(php|php5)?$
roothtml;
indexindex.html index.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;

9.3.4 重启nginx并检查
[root@site101 conf]# /usr/local/php/sbin/php-fpm -t
[root@site101 conf]# /usr/local/nginx/sbin/nginx -s reload
9.4、Nginx目录与配置文件
9.4.1 Nginx目录结构及说明
[root@site101 nginx]# tree -L 2
.
├── client_body_temp
├── conf#Nginx所有配置文件目录
│├── fastcgi.conf#fastcgi配置文件
│├── fastcgi.conf.default
│├── fastcgi_params#fastcgt的参数配置文件
│├── fastcgi_params.default
│├── koi-utf
│├── koi-win
│├── mime.types
│├── mime.types.default
│├── nginx.conf#Nginx主配置文件
│├── nginx.conf.default
│├── scgi_params
│├── scgi_params.default
│├── uwsgi_params
│├── uwsgi_params.default
│└── win-utf
├── fastcgi_temp
├── html#Nginx首页文件配置目录
│├── 50x.html
│├── index.html
│└── index.php
├── logs
│├── access.log
│├── error.log
│└── nginx.pid
├── proxy_temp
├── sbin#Nginx的所有命令配置文件
│└── nginx#Nginx启动命令
├── scgi_temp
└── uwsgi_temp
9.4.2 Nginx主配置文件说明
[root@site101 nginx]# tree -L 2
[root@site101 mhr]# egrep -v .*#|$ /usr/local/nginx/conf/nginx.conf
worker_processes1; #工程进程数
events#events模块
worker_connections1024; #并发连接数

http#http主配置
includemime.types; #文件扩展名与文件类型映射表
default_typeapplication/octet-stream; #默认文件类型
sendfileon; #开启高效文件传输模式
keepalive_timeout65; #配置长连接超时时间,默认为秒
server#虚拟主机
listen80; #监听端口
server_namelocalhost; #域名,可以多个,空格隔开
location /#location模块
roothtml; #默认站点目录
indexindex.html index.htm; #默认首页文件

error_page500 502 503 504/50x.html; #定义错误文件
location = /50x.html
roothtml;



9.5、配置Nginx虚拟主机
Nginx支持基于域名、IP、端口的虚拟主机
9.5.1 配置基于域名的虚拟主机
1、配置站点目录
Nginx默认站点目录是html,生产环境不建议使用
[root@site101 /]# mkdir -p /www/web/,blog/
[root@site101 /]# tree www
www
├── blog
└── web
[root@site101 /]# chown -R nginx.nginx /www/
2、配置默认站点首页
[root@site101 /]# echo "welcome to kele web-server" > > /www/web/index.html
[root@site101 /]# echo "welcome to kele blog-server" > > /www/blog/index.html
3、配置基于域名的虚拟主机
Nginx增加多个server标签
################################
server
listen80;
server_name www.site67.com *.site57.com;
location /
root /www/web;
index index.html;


server
listen80;
server_name blog.site67.com;
location /
root /www/blog;
index index.html;


################################
9.5.2 配置基于IP的虚拟主机
1、对ens33添加新的IP
[root@site101 mhr]# ifconfig ens32:2 192.168.31.69 netmask 255.255.255.0 up #临时
[root@site101 mhr]# echo "ifconfig ens32:2 192.168.31.69 netmask 255.255.255.0 up" > > /etc/rc.local #写入自启动脚本文件
2、Nginx配置基于IP的虚拟主机
################################
server
listen80;
server_name 192.168.31.67;
location /
root /www/web;
index index.html;


server
listen80;
server_name 192.168.31.68;
location /
root /www/blog;
index index.html;


################################
9.5.3 配置基于端口的虚拟主机
################################
server
listen8001;
server_name 192.168.31.67;
location /
root /www/web;
index index.html;


server
listen8002;
server_name 192.168.31.67;
location /
root /www/blog;
index index.html;


################################
9.6、优化Nginx配置
为提高配置效率,减少错误率和宕机率,并方便运维和操作。可以对配置文件进行优化。
9.6.1 精简主配置文件
[root@site101 conf]# egrep -v "#|^$" nginx.conf > > nginx.conf.new
#将去掉注释、空间之后的内容重定向到一个新的配置文件
[root@site101 conf]# mv nginx.conf.new nginx.conf
#将新生成文件替换为主配置文件
9.6.2 拆分主配置文件
生产环境虚拟主机可能有很多,因此频繁的修改主配置文件,很不方便。因此将配置文件拆分为主配置文件和虚拟主机配置文件两部分。
1、创建虚拟主机配置文件存放目录
[root@site101 conf]# mkdir vhost
2、拆分主配置文件
[root@site101 conf]# cp nginx.conf ./vhost/www.conf
[root@site101 conf]# cp nginx.conf ./vhost/blog.conf
3、修改主配置文件
#####修改后的主配置文件如下############
worker_processes1;
events
worker_connections1024;

http
includemime.types;
default_typeapplication/octet-stream;
sendfileon;
keepalive_timeout65;
include vhost/*.conf;

######################################
4、修改虚拟主机配置文件
[root@site101 conf]# cat ./vhost/www.conf
server
listen80;
server_namewww.site67.com;
location /
root/www/web;
indexindex.html;


[root@site101 conf]# cat ./vhost/blog.conf
server
listen80;
server_nameblog.site67.com;
location /
root/www/blog;
indexindex.html;


5、检查语法并重启服务
[root@site101 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.20.2//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.20.2//conf/nginx.conf test is successful
[root@site101 conf]# /usr/local/nginx/sbin/nginx -s reload
错误:
nginx: [error] invalid PID number "" in "/usr/local/nginx-1.20.2//logs/nginx.pid"
解决方法:
[root@site101 sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@site101 sbin]# /usr/local/nginx/sbin/nginx -s reload
9.6.3 开启日志功能
1、主配置文件开启日志功能
worker_processes1;
events
worker_connections1024;

http
includemime.types;
default_typeapplication/octet-stream;
sendfileon;
keepalive_timeout65;
log_formatcommonlog$remote_addr
  • log_formatcommonlog$remote_addr - $remote_user [$time_local] "$request"
$status $body_bytes_sent "$http_referer"
"$http_user_agent" "$http_x_forwarded_for";
include vhost/*.conf;

注意:include vhost/*.conf; 虚拟主机配置文件的包含配置要放在日志模块配置行的下面。
2、虚拟主机配置文件,增加日志存储目录及文件名
server
listen80;
server_nameblog.site67.com;
location /
root/www/blog;
indexindex.html;

access_log /usr/local/nginx/logs/access_blog.log commonlog;

9.6.4 配置nginx gzip压缩功能
配置nginx gzip可以提高传输速率和节省服务器带宽,针对全局就修改主配置文件,针对某个虚拟主机,则只需要修改虚拟主机配置文件。
#gzip
gzip on; #开启压缩功能
gzip_min_length 1K; #设置允许压缩页面的最小字节数
gzip_buffers 4 16k; #缓存空间大小
gzip_http_version 1.0; #版本
gzip_comp_level 2; #压缩级别
gzip_types text/plain application/X-javascript text/css application/xml; #压缩文件类型
gzip_vary on; #压缩标识
gzip_disable "MSIE [1-6]\\."; #IE1-6不压缩
测试压缩效果
curl -I -H "Accept-Encoding: gzip, deflate" "http://10.10.49.23:8080/news/login.jsp"
测试结果
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Mon, 24 Aug 2015 06:32:45 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: JSESSIONID=F0C060FA46D81BC87310765B85EFD857; Path=/news/; HttpOnly
Content-Encoding: gzip
9.6.5 配置expires缓存功能
缓存是服务器不更新文件的情况下,将某些文件缓存到客户端本地的期限。用户第二访问这些文件的时候,访问的是本地缓存内容。
expires缓存配置如下:
[root@site101 mhr]# vim /usr/local/nginx/conf/vhost/www.conf
location ~ .*\\.(gif|jpg|jpeg|png|bmp|ico)$
root /www/web;
expires 3d;

9.6.6 优化Nginx错误页面
创建错误页面内容:
[root@site101 web]# echo "This page has been lost. Please try again later." > > errors.html
配置错误页面:
[root@site101 web]# vim /usr/local/nginx/conf/vhost/www.conf
server
......
error_page 403 404= /errors.html;
【Nginx的安装与配置】



    推荐阅读