Nginx web基础

Nginx web基础

一、Nginx介绍

  • 概述

# Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。

# 开源: 直接获取源代码
# 高性能: 支持海量并发
# 可靠: 服务稳定
  • Nginx特点

高性能,高并发

# nginx支持很高的并发,nginx在处理大量并发的情况下比其他web服务要快

轻量且高扩展性

#轻量
功能模块少,只保留核心模块,其他代码模块化 (易读,便于二次开发,对于开发人员非常友好)

#高扩展性
需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块

高可靠性

只要不过分几乎不会出现问题
其他的web服务需要每隔一段时间进行重启,nginx不需要
nginx的宕机时间,是99999级别

支持热部署

nginx可以再运行期间,更新迭代,代码部署

大多数公司都在用nginx

1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的

2.适合当前主流架构趋势, 微服务、云架构、中间层

3.统一技术栈, 降低维护成本, 降低技术更新成本。

Nginx使用的是Epool网络模型

Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起一次请求,epool模型会直接进行处理,效率高效,并无连接限制。
  • 其他的web服务

1.apache:httpd,最早期使用的web服务,性能不高,操作难
2.nginx:
	tengine:Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性
	openresty-nginx:OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
3.IIS:windows下的web服务
4.lighttpd:是一个德国人领导的开源 Web 服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的 Web Server 环境。具有非常低的内存开销,CPU 占用率低,效能好,以及丰富的模块等特点。
5.GWS:google web server
6.BWS:baidu web server

Tomcat
Resin
weblogic
Jboss

二、Nginx安装

  • 安装方式

1.epol源安装
2.官方源安装
3.源码包安装
  • epol源安装

[root@web03 ~]# yum install -y nginx
  • 官方源安装

配置官方源

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

安装依赖

yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

安装nginx

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

配置nginx

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

启动服务

1.方法一:
[root@web01 ~]# systemctl start nginx

2.方法二:
[root@web01 ~]# nginx

检查启动

1.方式一
[root@web01 ~]# systemctl status nginx

2.方式二:
[root@web01 ~]# ps -ef | grep nginx

3.方式三:
[root@web01 ~]# netstat -lntp | grep 80

4.方式四:
直接访问网站  http://10.0.0.7/

5.方式五
[root@web01 ~]# curl 10.0.0.7

6.方式六:
[root@web01 ~]# nginx -v

nginx常用命令

1.nginx启动
    1)方法一:
    [root@web01 ~]# systemctl start nginx
    2)方法二:
    [root@web01 ~]# nginx
    
#注意:使用什么方式启动的,就使用对应的方式关闭
2.nginx停止
    1)方法一:
    [root@web01 ~]# systemctl stop nginx
    2)方法二:
    [root@web01 ~]# nginx -s stop
    
3.nginx重启
	1)方法一:
	[root@web01 ~]# systemctl restart nginx
	
4.nginx重载,重新加载配置文件
	1)方法一:
	[root@web01 ~]# systemctl reload nginx
	2)方法二:
	[root@web01 ~]# nginx -s reload
	
5.加入开机自启
	[root@web01 ~]# systemctl enable nginx
	
#Centos6:
	启动:nginx
		service nginx start
		/etc/init.d/nginx start
	加入开机自启:
		chkconfig nginx on
  • 源码包安装

下载安装包

[root@web02 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

解压

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

创建用户

[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

生成

[root@web02 ~]# cd nginx-1.16.1
[root@web02 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-1.16.1 --user=www --group=www --without-http_gzip_module

编译安装

[root@web02 nginx-1.16.1]# make && make install

做软连接

[root@web02 nginx-1.16.1]# ln -s /usr/local/nginx-1.16.1 /usr/local/nginx

配置环境变量

[root@web02 ~]# vim /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin

[root@web02 ~]# source /etc/profile

启动nginx

#启动时没有办法使用system管理,需要我们自己配置
[root@web02 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]

#启动
[root@web02 ~]# systemctl start nginx
  • nginx服务添加模块

1.安装依赖
[root@web02 nginx-1.16.1]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools 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_modulCe --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
  • 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

7.查看版本
[root@web02 ~]# nginx -v
nginx version: nginx/1.18.0

三、Nginx相关文件

为了让大家更清晰的了解Nginx软件的全貌,可使用rpm -ql nginx查看整体的目录结构及对应的功能,如下表格整理了Nginx比较重要的配置文件
  • Nginx主配置文件

路径类型作用
/etc/nginx/nginx.conf配置文件nginx主配置文件
/etc/nginx/conf.d/default.conf配置文件默认网站配置文件
  • Nginx代理相关参数文件

路径类型作用
/etc/nginx/fastcgi_params配置文件Fastcgi代理配置文件
/etc/nginx/scgi_params配置文件scgi代理配置文件
/etc/nginx/uwsgi_params配置文件uwsgi代理配置文件
  • Nginx编码相关配置文件

路径类型作用
/etc/nginx/win-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-win配置文件Nginx编码转换映射文件
/etc/nginx/mime.types配置文件Content-Type与扩展名
  • Nginx管理相关命令

路径类型作用
/usr/sbin/nginx命令Nginx命令行管理终端工具
/usr/sbin/nginx-debug命令Nginx命令行与终端调试工具
  • Nginx日志相关目录与文件

路径类型作用
/var/log/nginx目录Nginx默认存放日志目录
/etc/logrotate.d/nginx配置文件Nginx默认的日志切割

四、nginx配置文件

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

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

[root@web01 ~]# cat /etc/nginx/nginx.conf 
##################核心模块#################### ---》控制的是nginx的启动
user  www; #指定启动的用户

worker_processes  1; #nginx的worker进程的数量。 #worker越多工作效率越快和cpu核心数有关

#指定错误日志存放的路径以及记录的级别 debug/info/notice/warn/error/emerg/
error_log  /var/log/nginx/error.log warn;
#指定pid文件
pid        /var/run/nginx.pid;

########################事件驱动模块#################
events {
	#每个worker工作进程的最大连接数
    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;
	#开启压缩
    gzip  on;
	#包含网站的配置文件
    include /etc/nginx/conf.d/*.conf;
    
    #一个server表示一个网站
    server {
    	#监听端口
        listen       80;
        #网站提供的域名
        server_name  localhost;
        #字符集
        charset utf8;
        #匹配、控制访问的网站站点
        location / {
        	#指定站点目录
            root   /usr/share/nginx/html;
            #指定默认访问的页面
            index  index.html index.htm;
        }
    }
}

五、搭建小游戏

编写史上最简单配置

[root@web01 ~]# vim /etc/nginx/conf.d/game.conf
server {
    listen 80;
    server_name localhost;
    #server_name www.game.com;

    location / {
        root /code/tuixiangzi;
        index index.html;
    }
}

检查配置文件

[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/

创建站点目录

[root@web01 ~]# mkdir /code/

上传代码包

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

重载nginx

[root@web01 code]# systemctl restart nginx

访问页面玩游戏

六、再搭建一个游戏

编辑配置文件

[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

上传代码

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

配置windows下的hosts

10.0.0.7 www.game.com www.tank.com
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值