Nginx Web基础

Nginx Web基础

Nginx概述

Nginx是⼀个开源且⾼性能、可靠的Http Web服务、代理服务。
开源: 直接获取源代码
⾼性能: ⽀持海量并发
可靠: 服务稳定

Nginx特点

1.⾼性能、⾼并发

在并发特别⾼的时候,nginx的响应速度⽐其他的web服务快很多。

2.轻量且⾼扩展性

1.轻量:功能模块少,仅保留http模块和核⼼模块
2.⾼扩展性:模块需要哪个可以装哪个,还可以集成第三⽅的模块

3.⾼可靠性

其他的web服务运⾏⼀段时间需要重启,nginx不需要
nginx的宕机时间 9999、99999级别

4.⽀持热部署

可以在不停服务的情况下进⾏升级
nginx的master管理进程和worker的⼯作进程是分离的

5.互联⽹公司都在⽤nginx

nginx技术成熟,具备企业⾥所有需要的功能,负载,缓存,安全,web服务
统⼀技术栈,降低维护成本,降低技术难度

6.Nginx使⽤Epool⽹络模型

select:当⽤户发起⼀次请求,select模型就会进⾏⼀次遍历扫描,从⽽导致性能低下;
Epoll:当⽤户发起请求,epoll模型会直接进⾏处理,效率⾼效,并⽆连接限制。

7.其他的web服务

1.apache(httpd):早期使⽤,性能不⾼,上⼿很难;
2.nginx
 Tengine:淘宝基于nginx进⾏⼆次开发的产物
 openresty:基于nginx与lua的⾼性能web平台
3.lighttpd:消耗的内存和cpu较低,性能好,还有很多模块
4.IIS:windows的web服务
5.GWS:google web server
6.BWS:baidu web server

nginx应⽤场景

Nginx部署

安装方式

1.epol源安装       web01
2.官方源安装       web03
3.源码包安装       web03
 
#三种安装方式都需要安装nginx运行的依赖环境
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

1.epol源安装

yum install -y nginx

2.官⽅源安装

1)配置官⽅源

[root@web01 ~]# cd /etc/yum.repos.d/
[root@web01 yum.repos.d]# ll
total 8
-rw-r--r--. 1 root root 2523 Feb 11 15:29 CentOS-Base.repo
-rw-r--r--. 1 root root 664 Feb 11 15:29 epel.repo
[root@web01 yum.repos.d]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2)安装依赖包

[root@web01 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make
automake httpd-tools openssl-devel

3)yun安装nginx

[root@web01 ~]# yum install -y nginx

4)配置修改启动⽤

[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;

5)创建⽤户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

6)启动服务

#⽅法⼀:
[root@web01 ~]# nginx

#⽅法⼆:
[root@web01 ~]# systemctl start nginx

7)检查启动

#⽅式⼀:
[root@web01 ~]# systemctl status nginx

#⽅式⼆:
[root@web01 ~]# ps -ef | grep [n]ginx

#⽅式三:
[root@web01 ~]# netstat -lntp

#⽅式四:
访问⻚⾯ 10.0.0.7

#⽅式五:
[root@web01 ~]# curl 10.0.0.7

#⽅式六:
[root@web01 ~]# nginx -v
nginx version: nginx/1.16.1

8)nginx常⽤命令

1.nginx启动
#⽅法⼀:
[root@web01 ~]# nginx
#⽅法⼆:
[root@web01 ~]# systemctl start nginx
#注意:使⽤什么⽅式启动,就是⽤什么⽅式管理,不要混着⽤

2.nginx停⽌
#⽅法⼀:
[root@web01 ~]# nginx -s stop
#⽅法⼆:
[root@web01 ~]# systemctl stop nginx

3.nginx重启
[root@web01 ~]# systemctl restart nginx

4.nginx重新加载配置⽂件
#⽅法⼀:
[root@web01 ~]# nginx -s reload
#⽅法⼆:
[root@web01 ~]# systemctl reload nginx

5.检查nginx配置
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6.加⼊开机⾃启
[root@web01 ~]# systemctl enable nginx
#Centos6
nginx
/etc/init.d/nginx start
service nginx start
chkconfig nginx on

3.源码包安装

1)下载安装包

[root@web01 ~]# rz nginx-1.16.1.tar.gz

2)解压安装包

[root@web01 ~]# tar xf nginx-1.16.1.tar.gz

3)创建⽤户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

4)⽣成

[root@web01 ~]# cd nginx-1.16.1
[root@web01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-1.16.1 --
user=www --group=www --with-http_addition_module --withhttp_auth_request_modul

5)编译安装和做软连接

[root@web01 nginx-1.16.1]# make && make install
[root@web01 nginx-1.16.1]# ln -s /usr/local/nginx-1.16.1 /usr/local/nginx

6)启动

#启动的时候没有办法使⽤systemctl管理
[root@web01 local]# 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/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

7)配置环境变量

[root@web03 ~]# vim /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin
~                                      
[root@web03 ~]# source /etc/profile         #在当前bash环境下读取并执行/etc/profile中的命令

8)system管理配置

#源码包安装后没有办法使用system管理,需要我们自己配置
[root@web03 /usr/lib/systemd/system]# vim /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
 
[root@web03 ~]# systemctl daemon-reload                 #加载新的unit (*.service)配置文件

nginx升级

#升级版本
[root@web01 ~]# wget http://nginx.org/download/nginx-1.17.8.tar.gz
[root@web01 ~]# tar xf nginx-1.17.8.tar.gz
[root@web01 ~]# cd nginx-1.17.8/
[root@web01 nginx-1.17.8]# ./configure --prefix=/usr/local/nginx-1.17.8 --
user=www --group=www --with-http_addition_module --withhttp_auth_request_module
[root@web01 nginx-1.17.8]# make && make install
[root@web01 nginx-1.17.8]# rm -rf /usr/local/nginx && ln -s
/usr/local/nginx-1.17.8 /usr/local/nginx
------------------------------------------------------------
#添加模块
[root@web01 ~]# cd nginx-1.16.1/
[root@web01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-new-1.16.1
--user=www --group=www --with-http_addition_module --withhttp_auth_request_module --with-http_mp4_module
[root@web01 nginx-1.16.1]# make && make install
[root@web01 nginx-1.16.1]# rm -rf /usr/local/nginx && ln -s
/usr/local/nginx-new-1.16.1 /usr/local/nginx

三、Nginx命令

nginx #启动nginx。 等价于systemctl start 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 prefix #设置前缀路径(默认是:/usr/share/nginx/)
 
nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)
 
nginx -g directives #设置配置文件外的全局指令

四、Nginx服务平滑无感知添加模块和升级

1、模块解析

[root@web02 ~]# nginx -V        #显示版本和配置选项信息,然后退出
nginx version: nginx/1.18.0     
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie
 
############################内容解析#######################################
 
nginx version: nginx/1.18.0                 #nginx版本
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)    #
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments:
 --prefix=/etc/nginx            #nginx文件的安装路径,其它选项如果使用相对路径,那么以此路径为基础路径
 --sbin-path=/usr/sbin/nginx    #二进制程序的安装目录
 --modules-path=/usr/lib64/nginx/modules    #模块安装路径
 --conf-path=/etc/nginx/nginx.conf  #设置nginx的conf文件路径
 --error-log-path=/var/log/nginx/error.log  #设置nginx错误日志路径
 --http-log-path=/var/log/nginx/access.log  #设置nginx的访问日志路径
 --pid-path=/var/run/nginx.pid  #设置nginx的pid文件路径
 --lock-path=/var/run/nginx.lock    #设置lock文件临时存放的路径
 --http-client-body-temp-path=/var/cache/nginx/client_temp  #设置http客户端主体临时缓存路径
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp     #设置http反向代理临时路径
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp     #设置http的fastcgi临时缓存路径
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp #设置uwsgi临时目录
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp   #scgi临时存放目录
 --user=nginx   #设置启动 worker 进程时所使用的非特权用户名
 --group=nginx  #设置启动 worker 进程时所使用的非特权用户组名
 --with-compat
 --with-file-aio
 --with-threads
 --with-http_addition_module
 --with-http_auth_request_module
 --with-http_dav_module
 --with-http_flv_module
 --with-http_gunzip_module
 --with-http_gzip_static_module
 --with-http_mp4_module
 --with-http_random_index_module
 --with-http_realip_module
 --with-http_secure_link_module
 --with-http_slice_module
 --with-http_ssl_module
 --with-http_stub_status_module
 --with-http_sub_module
 --with-http_v2_module
 --with-mail
 --with-mail_ssl_module
 --with-stream
 --with-stream_realip_module
 --with-stream_ssl_module
 --with-stream_ssl_preread_module
 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
 --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
 --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

2.nginx服务添加模块

1.安装依赖
[root@web02 nginx-1.16.1]# yum install -y openssl openssl-devel
 
2.再生成一次
[root@web02 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-1.16.1-new --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
 
3.安装
[root@web02 nginx-1.16.1]# make && make install
 
4.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.16.1-new /usr/local/nginx
 
5.重启服务
[root@web02 nginx-1.16.1]# systemctl restart nginx

3.nginx升级

1.下载新版本的包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
 
2.解压
[root@web02 ~]# tar xf nginx-1.18.0.tar.gz
 
3.生成
[root@web02 nginx-1.18.0]# cd nginx-1.18.0
[root@web02 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module
 
4.编译安装
[root@web02 nginx-1.18.0]# make && make install
 
5.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.18.0 /usr/local/nginx
 
6.重启服务
[root@web02 ~]# systemctl restart nginx

Nginx相关⽂件

#查看nginx相关配置⽂件
[root@web01 ~]# rpm -ql nginx

1.Nginx主配置⽂件

路径                                    类型             作⽤
/etc/nginx/nginx.conf                  配置⽂件          nginx主配置⽂件
/etc/nginx/conf.d/default.conf         配置⽂件          默认⽹站配置⽂件

2.Nginx代理相关参数⽂件

路径                                    类型             作⽤
/etc/nginx/fastcgi_params              配置⽂件         Fastcgi代理配置⽂件(php)
/etc/nginx/scgi_params                 配置⽂件         scgi代理配置⽂件
/etc/nginx/uwsgi_params                配置⽂件         uwsgi代理配置⽂件(python)

3.Nginx编码相关配置⽂件

路径                                    类型             作⽤
/etc/nginx/win-utf                     配置⽂件          Nginx编码转换映射⽂件
/etc/nginx/koi-utf                     配置⽂件          Nginx编码转换映射⽂件
/etc/nginx/koi-win                     配置⽂件          Nginx编码转换映射⽂件
/etc/nginx/mime.types                  配置⽂件          Content-Type与扩展名

4.Nginx管理相关命令

路径                                    类型              作⽤
/usr/sbin/nginx                        命令              Nginx命令⾏管理终端⼯具
/usr/sbin/nginx-debug                  命令              Nginx命令⾏与终端调试⼯具

5.Nginx⽇志相关⽬录与⽂件

路径                                     类型             作⽤
/var/log/nginx                          ⽬录             Nginx默认存放⽇志⽬录
/etc/logrotate.d/nginx                  配置⽂件         Nginx默认的⽇志切割

Nginx配置⽂件详解

  1. Nginx主配置⽂件/etc/nginx/nginx.conf是⼀个纯⽂本类型的⽂件,整个配置⽂件是以区块的形式
    组织的。⼀般,每个区块以⼀对⼤括号{}来表示开始与结束,{} 内每⾏以 ; 结尾。
  2. Nginx主配置⽂件整体分为三块进⾏学习,分别是CoreModule(核⼼模块),EventModule(事件驱动
    模块),HttpCoreModule(http内核模块)
#---------------------------------核⼼模块-----------------------------
#启动⽤
user nginx;
#⼯作进程数量
worker_processes 1;
#错误⽇志 debug/info/notice/warn/error/crit/alter/emerg
error_log /var/log/nginx/error.log warn;
#pid⽂件
pid /var/run/nginx.pid;
#-----------------------------事件驱动模块-------------------------
events {
 worker_connections 1024;
}
#-----------------------------http内核模块--------------------------
http {
 #nginx包含的⽂件类型
 include /etc/nginx/mime.types;
 #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;
 tcp_nodelay on;
 
 #是否开启压缩
 #gzip on;
 
 #包含配置⽂件
 include /etc/nginx/conf.d/*.conf;
 
 #使⽤server配置⽹站,每个server代表⼀个⽹站(我们称之为虚拟主机)
 server {
 #监听端⼝
 listen 80;
 #提供的域名
 server_name _;
 
 #控制⽹站访问路径
 location / {
 #站点⽬录,指定存放⻚⾯的地址
 root /code;
 #默认访问的⻚⾯
 index index.html
 }
 }
 ...
 server {
 ...
 }
} 

搭建⼩游戏

1.编写史上最简单配置

[root@web01 ~]# vim /etc/nginx/conf.d/game.conf
server {
 listen 80;
 server_name localhost;
 location / {
 root /code;
 index index.html;
 }
}

2.检查配置文件

[root@web01 code]# nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
 
#因为网站冲突
[root@web01 code]# mv /etc/nginx/conf.d/default.conf /tmp/

3.创建站点⽬录

[root@web01 ~]# mkdir /code

3.上传代码包

[root@web01 code]# rz
[root@web01 code]# unzip tuixiangzi.zip
[root@web01 code]# mv HTML5 canvas小人推箱子小游戏 tuixinagzi

4.重载nginx

[root@web01 code]# nginx -s reload 注:以什么⽅式启动的就以什么⽅式管理
[root@web01 code]# systemctl restart nginx (建议使⽤system管理)

再搭建一个游戏

1.编辑配置文件

[root@web01 code]# vim /etc/nginx/conf.d/gametwo.conf 
server {
    listen 80;
    server_name www.tank.com;
 
    location / {
        root /code/tank;
        index index.html;
    }
}
 
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
 
[root@web01 code]# systemctl restart nginx

2.上传代码

[root@web01 code]# cd /code/
[root@web01 code]# rz
[root@web01 code]# unzip tankedazhan.zip
[root@web01 code]# mv jQuery坦克大战网页小游戏 tank

3.配置windows下的hosts

位置:C:\Windows\System32\drivers\etc
10.0.0.7 www.game.com www.tank.com
posted @ 2021-08-09 16:54  小丶凡  阅读(46)  评论(0编辑  收藏  举报
1