JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

新手学习第二阶段:Nginx服务安装配置及实战演练

wys521 2024-09-06 04:28:20 精选教程 27 ℃ 0 评论

一、Nginx介绍(官网地址:http://nginx.org)

1、nginx是什么?

nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。

nginx是一款轻量级的web服务器/反向代理服务器以及电子邮件代理服务器。其特点是占用内存少,并发能力强,实际中nginx的并发能力确实实在同类型的网页服务器中表现较好。

nginx相较于Apache具有占用内存少,稳定性高,并且依靠并发能力强,丰富的模块库以及友好灵活的配置而闻名。在Linux系统下,nginx使用"epoll"事件模型,得益于此,nginx在Linux系统下效率相当高。

2、常见用法:

1.web服务器软件httpd http协议

同类的web服务器软件:apache nginx iis lighttpd

2.代理服务器 反向代理

3.邮箱代理服务器 IMAP POP3 SMTP

4.负载均衡 LB loadblance

3、nginx架构的特点:

1.高可靠:稳定性 master进程 管理调度请求分发到哪一个worker >> worker进行响应请求 一master 多worker

2.热部署:①平滑升级 ②可以快速重载配置

3.高并发:可以同时响应更多的请求,事件epoll模型

4.响应快:尤其在处理静态文件上,响应速度很快 sendfile

5.低消耗:cpu、内存 1w请求,占用内存2-3MB

6.分布式支持:反向代理,七层负载均衡

4、Nginx与Apache之间对比

 网络模型: 
			select 性能最低 
			poll 性能稍好 
			epoll 性能最强 
Apache:select 
Nginx:epoll

5、nginx安装方法

1.yum方式安装

Yum安装nginx,先到nginx官网,下载官方源。
第一步:
cd /etc/yum.repos.d
vi nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
第二步:
yum clean all
第三步:
yum makecache
第四步:
cd /etc/yum.repos.d
mv epel.repo epel.repo.bak
第五步:
yum install nginx -y


注:安装官方源,必须先注释epel源,在进行yum安装nginx

2.epel源安装

 yum install -y

3.编译安装

 官方稳定版:http://nginx.org/download/nginx-1.20.1.tar.gz
第一步:下载官方包,上传到Linux服务器
解压Nginx压缩包
tar xvf nginx-1.20.1.tar.gz
 第二步:进入nginx目录
cd nginx-1.20.1
设置系统配置参数,执行下面
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_v2_module --with-stream
编译
make
make install
或
make && make install

参数说明:

参数

作用

--prefix

编译安装到的软件目录

--user

worker进程运行用户

--group

worker进程运行的用户组

--with-http_ssl_module

支持https 需要安装 pcel-devel 依赖

--with-http_stub_status_module

基本状态信息显示,查看请求数、连接数

--with-http_realip_module

定义客户端地址和端口为header头信息,常用于反向代理后的真是IP获取

--with-http_v2_module

配置GOLANG语言时使用

--with-stream

启用TCP/UDP代理服务

目录介绍: /usr/local/nginx

目录

作用

conf

配置文件

html

网站默认目录

logs

日志

sbin

可执行文件[软件启动、停止、重启等操作]

4.nginx目录操作参数

注:编译安装的nginx,默认命令不在系统环境变量中,无法直接使用sytemctl。需要加入到环境变量中。

systemctl status nginx
Unit nginx.service could not be found.

nginx加入环境变量:

 vi /etc/profile 
export PATH=$PATH:/usr/local/nginx/sbin 
执行: source /etc/profile

nginx加入system系统管理(切记:文件中内容前面不能有空格)

 #进入配置文件进行添加内容
 vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
#重载system服务并启动
systemctl daemon-reload
systemctl start nginx
 cd /usr/local/nginx 
   ./nginx -h

命令执行后显示:

 [root@web03 sbin]# ./nginx -h
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
    #查看帮助
  -?,-h         : this help
    #查看版本并推出
  -v            : show version and exit
  	#查看版本和配置选项并推出
  -V            : show version and configure options then exit
  #检测配置文件语法并推出
  -t            : test configuration and exit
  #检测配置文件语法打印并推出
  -T            : test configuration, dump it and exit
  #在配置测试期间禁止显示非错误信息
  -q            : suppress non-error messages during configuration testing
  #发送信号给主进程 stop强制推出 quit优雅推出 reopen重开日志 reload重载配置
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  #设置nginx目录 $prefix路径
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  #设置错误日志的路径
  -e filename   : set error log file (default: logs/error.log)
  #指定启动使用的配置文件
  -c filename   : set configuration file (default: conf/nginx.conf)
  #在配置文件之外设置全局指令
  -g directives : set global directives out of configuration file

注:常用

-s参数控制管理nginx服务

-V参数查看nginx开启的模块和编译参数

-t参数检测配置文件是否错误

编译安装nginx时,会遇到以下几个问题点:


解决依赖:

 yum install prce-devel zlib-devel openssl-devel

注:安装openssl依赖时,默认把zlib-devel 已安装上。

依赖安装成功:

安装步骤及脚本:

 #!/bin/bash
#编译安装Nginx
nginx_install(){
#创建软件运行用户
`id www` &>>/dev/null
if [ $? -ne 0 ];then
   useradd -s/sbin/nologin -M www
fi
#安装依赖
yum -y install pcre-devel zlib-devel openssl-devel
#编译安装
cd /root/soft
tar xvf nginx-1.20.1.tar.gz
cd nginx-1.20.1.tar.gz
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_v2_module --with-stream && make && make install
}
#脚本开始时间
start_time=`date +%s`
#执行的脚本代码
nginx_install
#脚本结束时间
end_time=`date +%s`
#脚本执行花费时间
const_time=$((end_time-start_time))
echo 'Take time is: '$const_time's'

6、Nginx配置文件说明

路径:/etc/nginx/nginx.conf

# 工作进程启动用户
user  nginx;
# 启动的worker进程数
worker_processes  auto;
# 指定错误日志的路径
error_log  /var/log/nginx/error.log notice;
# 指定nginx进程的PID
pid        /var/run/nginx.pid;
# 配置事件
events {
    # worker进程中最大的连接数
    worker_connections  1024;
}

# http配置模块
http {
	# include 是讲其他文件导入进来
    include       /etc/nginx/mime.types;
	
	# default_type 指定nginx处理文件的默认类型
    default_type  application/octet-stream;

	# 定义日志的格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
	
	# 定义日志路径
    access_log  /var/log/nginx/access.log  main;

	# 高效读取文件
    sendfile        on;
    #tcp_nopush     on;
	
	# 长连接的超时时间
	keepalive_timeout  65;

	# 设置GZIP压缩
    #gzip  on;

	# 加载其他的配置文件
    include /etc/nginx/conf.d/*.conf;
}
说明:include /etc/nginx/conf.d/*.conf;
加载其他配置的好处在于,方便管理,不同网站使用单独一个配置文件,修改起来也比较方便
/etc/nginx/conf.d/	配置文件都存放于这个目录下
添加完新的配置文件后执行命令:nginx -t	查看配置文件是否正确
[root@web01 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful 

7、实战演练,网页版小蝌蚪聊天室

源码下载地址:

链接:https://pan.baidu.com/s/1ZUqGUOpVXXZVhlEqD8cCGw

提取码:xm5m

第一步:
上传源码到nginx目录
/usr/share/nginx/
第二步:
添加nginx配置文件
/etc/nginx/conf.d
vim xkd.conf
server{
    #域名,此域名非正式域名,需要到windows电脑下更改hosts文件
	server_name test.xkd.com;
 	#监听端口
	listen 80;
	location / {
 		#指定源码路径
		root /usr/share/nginx/xkd;
  		#指定网站方式默认首页
		index index.html;
	}
}

第三步:
执行命令:nginx  -t
检测配置文件是否正确
第四步:
重启nginx服务
systemctl restart nginx
第五步:
更改本地hosts文件
C:\Windows\System32\drivers\etc\hosts
本机IP test.xkd.com

注:nginx日志地址:/var/log/nginx/

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表