07 Nginx 基础 三种nginx安装方式

Nginx Web 基础

一、Nginx介绍

1.概述

Nginx是一个开源且高性能、可靠的http web服务、代理服务

2.Nginx特点

1.高性能,高并发
2.轻量且高扩展性
3.高可靠性
4.支持热部署,nginx可以在运行期间,更新迭代,代码部署
5.大多数公司都在用nginx
6.nginx使用的是epool网络模型

1.开源
2.Nginx Web服务器
3.Nginx是俄罗斯一个程序员
4.Nginx还是代理

3.Nginx和Apache之间对比

网络模型:select   性能最低
        poll     性能稍好
        epoll   性能最强
Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epoll: 当用户发起一次请求,epoll模型会直接进行处理,效率高效,并无连接限制。

Apache :select
Nginx :epoll
       
       

二、安装Nginx

1.yum 安装

1.官网安装https://nginx.org/ (推荐)

官网路径:nginx.org——documentation——installing nginx——packages——RHEL/CentOS——/etc/yum.repos.d/nginx.repo

1.[root@web02 ~]# vim /etc/yum.repos.d/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

2.​如果安装官方源,epel源必须注释掉(避免epel源影响)
cd /etc/yum.repos.d/
gzip epel.repo
3.yum清理缓存
[root@web01 ~]# yum clean all
[root@web01 ~]# yum makecache

4.安装nginx
[root@web01 ~]# yum install nginx -y

5.测试是否安装成功
[root@web01 ~]#nginx -v

6.查看nginx的配置文件
rpm -qc nginx

7.启动nginx
systemctl start nginx
 


2.epel 安装 (将web01的配置同步到web02 (scp /etc/nginx/nginx.conf 172.16.1.7:/etc/nginx))

1.epel镜像地址:https://developer.aliyun.com/mirror/epel?spm=a2c6h.13651102.0.0.3e221b113u4zK6

2.下载epel源:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

3.yum清空缓存
[root@web02 ~]# yum clean all
[root@web02 ~]# yum makecache

4.安装nginx
[root@web02 ~]# yum install nginx

web02要使用web01服务器的nginx的配置文件
scp /etc/nginx/nginx.conf root@172.16.1.8:/etc/nginx/nginx.conf

5.启动nginx
systemctl start nginx

3.源代码编译安装 https://nginx.org/download/nginx-1.20.1.tar.gz

1、下载源代码包
[root@web03 ~]# wget https://nginx.org/download/nginx-1.20.1.tar.gz

2、解压
    [root@web03 ~]# tar -xf nginx-1.20.1.tar.gz
   
3、进入nginx目录并且设置系统配置参数
[root@web03 nginx-1.20.1]# ./configure --help (查看参数)
[root@web03 nginx-1.20.1]# ./configure --with-http_ssl_module --with-http_v2_module --with-stream

--with-http_ssl_module # 配置HTTPS时使用
--with-http_v2_module # 配置GOLANG语言时使用
--with-stream                # 启用TCP/UDP代理服务

4.开始编译
[root@web03 nginx-1.20.1]# make

5、安装
[root@web03 nginx-1.20.1]# make install

现在在命令行输入nginx,报错,需要加入环境变量

 

 

6、 加入环境变量
[root@web03 nginx]# vi /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin

[root@web03 nginx]# source /etc/profile

7.加入system系统管理(重要:一定要顶格写)
[root@web03 sbin]# 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

8、重载system服务并启动
[root@web03 sbin]# systemctl daemon-reload
[root@web03 sbin]# systemctl start nginx


错误:
1、./configure: error: the HTTP rewrite module requires the PCRE library.

yum install pcre pcre-devel -y # pcre_devel是拓展包

2、./configure: error: SSL modules require the OpenSSL library.

yum install openssl openssl-devel -y

三、Nginx常用命令详讲


nginx    #启动nginx。 等价于systemctl start nginx

nginx -s #向nginx发送信号

nginx -s reopen #重启Nginx。   等价于systemctl restart nginx

nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx。 等价于systemctl reload nginx

nginx -s stop #强制停止Nginx服务。 等价于systemctl stop nginx

nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)

nginx -t #检测配置文件是否有语法错误,然后退出

nginx -?,-h #打开帮助信息

nginx -v #显示版本信息并退出

nginx -V #显示版本和配置选项信息,然后退出

nginx -V 2>&1 | sed "s/\s\+--/\n --/g" #模块分行输出,格式化输出

killall nginx #杀死所有nginx进程

systemctl enable nginx  #加入开机自启
  Centos6:
      启动:nginx
           service nginx start
          /etc/init.d/nginx start
      加入开机自启:
          chkconfig nginx on

nginx -T #检测配置文件是否有语法错误,转储并退出

nginx -q #在检测配置文件期间屏蔽非错误信息(静默输出错误信息)

nginx -p #指定运行的家目录

nginx -p prefix #设置前缀路径(默认是:/usr/share/nginx/)

nginx -e #设置错误日志的路径

nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)

nginx -g directives #设置配置文件外的全局指令(临时设置配置项)

 

四、Nginx的配置文件(vi /etc/nginx/nginx.conf)

 

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。

Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)

 

Nginx的进程模型
  角色:
      master :负责监控worker进程
      worker :负责处理HTTP请求

##################核心模块###############
#工作进程启动用户
user nginx;
#启动的worker进程数(worker进程数如果设置成auto,则会跟cpu数量保持一致)
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内核模块
http {
#包含nginx可识别的文件类型(将其他文件导入进来)
include /etc/nginx/mime.types;
#当nginx不识别文件类型的时候,默认下载(指定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;

game.conf
# 每一个server都代表一个网站(nginx是支持多个网站)
server {
# 指定当前网站的域名
server_name www.baidu.com;
#字符集
charset utf8;
# 指定当前网站监听的端口
listen 80;
# 指定一个访问路径(配置、控制访问的网站站点)
location / {
# 指定站点目录
root /opt/html;
# 指定默认访问的页面
index index.html;
}

五、案例:搭建小游戏

1.上传代码,解压uzip 解压到/usr/share/nginx

 

image-20211026190529502

2.增加网站的配置

[root@web02 nginx]# cd /etc/nginx/conf.d

1.游戏1:[root@web02 nginx]# vim /etc/nginx/conf.d/game1.conf

server {
server_name game1.test.com;
listen 80;
location / {
root /usr/share/nginx/html5-mario;
index index.html;
}
}

2.游戏2:[root@web02 nginx]# vim /etc/nginx/conf.d/game2.conf

server {
server_name game2.test.com;
listen 80;
location / {
root /usr/share/nginx/jiaoben1765;
					index index.html;
}
}
3.测试nginx配置文件是否正确
[root@web02 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4、启动nginx
[root@web02 conf.d]# systemctl restart nginx
5.编辑Windows的hosts文件
C:\Windows\System32\drivers\etc

 

image-20211026191351083

如果无法修改hosts文件,请修改权限

 

image-20211026191441097

6.测试
http://game1.test.com/
或者
http://game2.test.com/

 

 

image-20211026191642018

image-20211026191717781i

 

posted @ 2021-10-26 19:27  甜甜de微笑  阅读(406)  评论(0编辑  收藏  举报