Nginx的应用之安装配置

一、Nginx简述

Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定
我们为什么选择 Nginx服务
Nginx非常轻量
功能模块少 (源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)
代码模块化 (易读,便于二次开发,对于开发人员非常友好)
互联网公司都选择Nginx
1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的
2.适合当前主流架构趋势, 微服务、云架构、中间层
3.统一技术栈, 降低维护成本, 降低技术更新成本。
Nginx采用Epool网络模型,Apache采用Select模型
Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。
Nginx 典型应用场景

二、快速安装

Nginx软件安装的方式有很多种
1.源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐)
2.epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)
3.官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读,推荐)

官方仓库

1.安装Nginx软件所需依赖包

[root@web ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

2.配置nginx官方yum源

[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

3.安装Nginx服务,启动并加入开机自启

[root@web ~]# yum install nginx -y
[root@web ~]# systemctl enable nginx
[root@web ~]# systemctl start nginx

4.通过浏览器访问该服务器ip或url地址 10.0.0.7

5.检查Nginx软件版本以及编译参数

[root@web ~]# nginx -v
nginx version: nginx/1.16.0
[root@web ~]# nginx -V

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

(1)Nginx主配置文件

路径类型作用
/etc/nginx/nginx.conf 配置文件 nginx主配置文件
/etc/nginx/conf.d/default.conf 配置文件 默认网站配置文件

 

 

 

 

(2)Nginx代理相关参数文件

路径类型作用
/etc/nginx/fastcgi_params 配置文件 Fastcgi代理配置文件
/etc/nginx/scgi_params 配置文件 scgi代理配置文件
/etc/nginx/uwsgi_params 配置文件 uwsgi代理配置文件

 

 

 

 

 

(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默认配置

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。
Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)

CoreModule核心模块

user nginx;                       #Nginx进程所使用的用户
worker_processes 1;             #Nginx运行的work进程数量(建议与CPU数量一致或auto)
error_log /log/nginx/error.log  #Nginx错误日志存放路径
pid /var/run/nginx.pid          #Nginx服务运行后产生的pid进程号

events事件模块

events {            
    worker_connections 25535;  #每个worker进程支持的最大连接数
    use epoll;                  #事件驱动模型, epoll默认
}

http内核模块

http {  #http层开始
...    
    #使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
    server {
        listen       80;            #监听端口, 默认80
        server_name  localhost;       #提供的域名
        access_log  access.log;     #该网站的访问日志
        #控制网站访问路径
        location / {
            root   /usr/share/nginx/html;   #存放网站源代码的位置
            index  index.html index.htm;    #默认返回网站的文件
        }
    }
    ...
    #第二个虚拟主机配置
    'server' {
    ...
    }
    
    include /etc/nginx/conf.d/*.conf;  #包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件

}   #http层结束

http server location扩展了解项

http{}层下允许有多个Server{}层,一个Server{}层下又允许有多个Location
http{} 标签主要用来解决用户的请求与响应。
server{} 标签主要用来响应具体的某一个网站。
location{} 标签主要用于匹配网站具体URL路径

 

四、Nginx网站配置

(1)新增nginx配置文件

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

    location / {
        root /data/www/game;
        index index.html;
    }
}

(2)放置项目的源代码文件至nginx配置文件root指定的目录

[root@web01 conf.d]# mkdir /data/www/game && cd /data/www/game
[root@web01 game]# echo "This is my game page" > /data/www/game/index.html
[root@web01 game]# ls
  index.html

(3)检查nginx的语法是否存在错误

[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

(4)重载Nginx [reload|restart]

[root@web01 code]# systemctl reload nginx

 

posted @ 2019-08-24 15:59  zh_Revival  阅读(176)  评论(0编辑  收藏  举报