目录

  • 1. 常见的http web服务
  • 2. 为什么选择Nginx
  • 3.Nginx部署
    • 3.1.Nginx安装方式:
    • 3.2.安装Nginx 1.16.1(yum)
      • 3.2.1 配置官方Nginx官方yum源
      • 3.2.2 查看安装源
      • 3.2.3 安装Nginx
      • 3.2.4 Nginx配置文件
    • 3.3 问题:安装问题
    • 3.4.安装nginx 1.16.0(源代码)
      • 3.4.1 官方下载tar包:http://nginx.org/
      • 3.4.2 解压软件包
      • 3.4.3 https依赖于加密池
      • 3.4.4 编译安装
  • 4.Nginx配置文件.conf
    • 4.1 需了解扩展项
    • 4.2 CoreModule核心模块
    • 4.3 events事件模块
    • 4.4 http内核模块
  • 5.Nginx配置网站

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

  • 开源:直接获取源代码
  • 高可靠:支持海量并发
  • 高性能:服务稳定
  • 轻量:占用资源少

1. 常见的http web服务

  • Http 由apache基金会
  • IIS 微软服务版
  • GWS Google开发
  • Openrestry 基于nginx+lua
  • Tengline 淘宝基于Nginx开发

2. 为什么选择Nginx

Nginx非常轻量功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)

代码模块化(易读,便于二次开发,对于开发人员非常友好)

  • Nginx技术成熟,国内公司基本大规模使用

    ​ 适用于当前主流架构趋势,微服务、云架构、之间层

    ​ 统一技术 ,降低维护成本,降低技术更新成本

  • Nginx采用Epoll网络模型,Apache采用Select模型

    ​ Select:当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能降低

    ​ Epoll:当用户发起请求,epoll模型会直接进行处理,效率高效,并无连接限制

Nginx应用场景

  • ​ 静态服务: html | jgp | png …

    ​ Nginx缓存

  • ​ 代理服务:

    ​ 正向代理:内访外

    ​ 反向代理:外访内和负载均衡的效果是一样的,但是工作方式有区别。

  • ​ 负载均衡:

  • ​ 缓存

  • ​ 安全服务:

    ​ 访问控制:基于ip地址,基于身份认证

    ​ WAF:DDOS攻击、CC攻击、SQL注入

  • ​ 架构

    ​ LNMP (linux、nginx、mysql、php)

    ​ LNMT(linux、ngxin、mysql、tomcat)

3.Nginx部署

3.1.Nginx安装方式:

  • epel源:版本低、功能少
  • 官方源:官方编译好,封装成rpm包,并提供yum源,(推荐)
  • 源代码:自己下载源代码,使用.configure/make/make install (安装复杂,费时)

3.2.安装Nginx 1.16.1(yum)

3.2.1 配置官方Nginx官方yum源

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

3.2.2 查看安装源

[root@nginx_web1 yum.repos.d]# yum list nginx
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
base: mirrors.aliyun.com
extras: mirrors.aliyun.com
updates: mirrors.aliyun.com
可安装的软件包
nginx.x86_64        1:1.16.1-1.el7.ngx          nginx-stabl

3.2.3 安装Nginx

[root@nginx_web1 yum.repos.d]# yum -y install nginx
[root@nginx_web1 yum.repos.d]# nginx -v                //小v查看版本号
nginx version: nginx/1.16.1
[root@nginx_web1 yum.repos.d]# nginx -V        //大V查看版本号及安装选项
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (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'
[root@nginx_web1 yum.repos.d]# cd /usr/lib64/nginx/modules/

3.2.4 Nginx配置文件

**Nginx主配置文件**
/etc/nginx
/etc/nginx/nginx.conf
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
**cgi、Fastcgi、Uwcgi配置文件**
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
**Nginx编码编码转换映射文件**
/etc/nginx/win-utf
/etc/nginx/koi-utf
/etc/nginx/koi-win
**http协议得Content-Type与扩展名**
/etc/nginx/mime.types
**配置系统守护进程管理器**
/usr/lib/systemd/system/nginx.service
**Nginx日志轮询,日志切割**      ******
/etc/logrotate.d/nginx
**Nginx终端管理命令**
/usr/sbin/nginx
/usr/sbin/nginx-debug
**Nginx模块目录**
/usr/lib64/nginx
/usr/lib64/nginx/modules
**Nginx默认站点目录**
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
**Nginx帮助手册**
/usr/share/man/man8/nginx.8.gz
/usr/share/doc/nginx-1.16.1
**Nginx的缓存目录**
/var/cache/nginx
**Nginx的日志目录**
Nginx/log/nginx

3.3 问题:安装问题

问题一:如果企业之前的Nginx都是通过源码安装,这是需要我们部署新的Nginx服务器,怎么实现

​ 解法:通过Nginx -v 获得版本、通过nginx -V 获得configuration选项

问题二:选项那么多,必须要配吗,网上搜得博文选项没那么长

​ 解法:选项是根据企业需求来指定,最好按官方来实现

3.4.安装nginx 1.16.0(源代码)

3.4.1 官方下载tar包:http://nginx.org/

3.4.2 解压软件包

tar xf nginx-1.16.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.16.0/
useradd -s /sbin/nologin nginx -M id nginx

3.4.3 https依赖于加密池

#安装依赖。
yum install pcre pcre-devel -y
yum install openssl openssl-devel -y  #https加密用他。

3.4.4 编译安装

–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
设定Nginx进程启动用户和组(安全)
–with-cc-opt 设置额外的参数将被添加到CFLAGS变量
–with-ld-opt 设置附加的参数,链接系统库
./configure  --prefix=/application/nginx-1.16.0/ 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'    //模块可以查看yum安装 nginx -V
make
make install
ln -s /application/nginx-1.16.0/ /application/nginx
/application/nginx/sbin/nginx
netstat -lntup|grep nginx默认启动
cd /application/nginx-1.16.0/sbin/
./nginx             启动
./nginx -s stop     关闭
./nginx -s quit     退出
./nginx -s reload   重新加载systemctl启动文件
cat > /lib/systemd/system/nginx.service <<-EOF
添加内容如下:
[Unit]
Description=nginx service
After=network.target [Service]
Type=forking
ExecStart=/application/nginx-1.16.0/sbin/nginx
ExecReload=/application/nginx-1.16.0/sbin/nginx -s reload
ExecStop=/application/nginx-1.16.0/sbin/nginx -s quit
PrivateTmp=true [Install]
WantedBy=multi-user.target
EOF
systemctl enable nginx
systemctl disable nginx

4.Nginx配置文件.conf

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

  1. CoreModule 核心模块 全局配置
  2. EventModule 事件驱动模块
  3. httpCoreModule http内核模块 局部

4.1 需了解扩展项

  • CoreModule层下可以有Event、HTTP
  • HTTP模块层允许有多个server层,server主要用于配置多个网站
  • Server层允许有多个Location,Location主要用于定义网站访问路径

4.2 CoreModule核心模块

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

4.3 events事件模块

events   {worker_connectios  1024;       #每个worker进程支持最大连接数量默认1024use opool;              #事件驱动模块,epoll默认
}

4.4 http内核模块

vim /etc/nginx/nginx.conf
http {include       /etc/nginx/mime.types;    //支持的文件类型路径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;       //访问日志keepalive_timeout  65;                    //长连接 超时间65秒 include /etc/nginx/conf.d/*.conf;      //所有配置文件路径}[root@nginx_web1 html]# egrep -v '^$|^.*#' /etc/nginx/conf.d/default.conf  //过滤注释信息
vim /etc/nginx/nginx/conf.d/default.confserver {listen 80;                       //监听端口号80server_name localhost;           //提供服务的域名或主机名#access_log  /var/log/nginx/host.access.log       //访问日志location  /   {root   /usr/share/nginx/html;      //访问网站代码路径index  index.html index.htm;      //服务器返回的默认页面文件}//指定代码,统一定义错误页面,错误代码重定向到新的Locationerror_page   500 502 503 504  /50x.html; location = /50x.html {root   /usr/share/nginx/html;}}[root@nginx_web1 conf.d]# vim oldboy.conf     //多站点可以分不同文件编写。前提主机名、端口、ip不能冲突server {listen 80server_name www.oldboy.comlocation  /   {root   /oldboy/wwwindex.php}}

5.Nginx配置网站

[root@nginx_web1 conf.d]# mv default.conf default.conf.bak
[root@nginx_web1 conf.d]# vim oldboy.conf
server {listen  80;server_name localhost;location / {root /html;index index.html;}
}
[root@nginx_web1 conf.d]# mkdir /html
[root@nginx_web1 conf.d]# echo "nihao123456778" > /html/index.html
[root@nginx_web1 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
systemctl restart nginx[root@nginx_web1 conf.d]# tail /var/log/nginx/access.log        //访问日志
[root@nginx_web1 conf.d]# tail /var/log/nginx/error.log            //错误日志信息

1. Nginx基本概述与部署相关推荐

  1. Kafka入门(一) 概述、部署与API的简单使用

    Kafka入门(一) 概述.部署与API的简单使用 MQ 传统架构的问题 MQ简介 MQ的优缺点 优点 缺点 同步与异步 P2P模式 订阅发布模式 Kafka概述 简介 特点 名称解释 Broker ...

  2. Kubernetes K8S之kube-prometheus概述与部署

    Kubernetes K8S之kube-prometheus概述与部署 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7. ...

  3. Ansible概述与部署及playbook剧本编写

    Ansible自动化运维管理工具的概述与部署 文章目录 Ansible自动化运维管理工具的概述与部署 一.Ansible简介 1.Ansible概述 2.ansible环境安装部署 3.ansible ...

  4. 使用nginx实现一个主机部署多域名指向不同docker项目

    使用nginx实现一个主机部署多域名指向不同docker项目 原文:使用nginx实现一个主机部署多域名指向不同docker项目  1,安装 docker yum install docker 使用D ...

  5. Django + Uwsgi + Nginx 实现生产环境部署1

    项目开发完毕后,需要将代码放到服务器上,这样用户才能访问.接下来我们一步一步来进行一波部署操作. 1. 简单粗暴 项目开发完毕,在部署之前需要再配置文件中将 ALLOWED_HOSTS配置设置为:当前 ...

  6. Nginx + uWSGI + flask + socketio 部署解决方案

    Nginx + uWSGI + flask + socketio 部署解决方案 参考文章: (1)Nginx + uWSGI + flask + socketio 部署解决方案 (2)https:// ...

  7. Linux下nginx的安装及部署

    Linux下nginx的安装及部署 一. nginx的安装 1. nginx安装需要很多依赖包(并不知道是干神马的,连接好网络,敲命令直接安便是了) 安装GCC 和GCC-C++(若没有安装gcc) ...

  8. nginx同一域名下部署多个vue项目

    -----------nginx同一域名下部署多个vue项目------------------------ 1.目的 实现nginx服务端同一域名下部署多个vue项目. 2. 实例 pmobile项 ...

  9. Redis(主从复制、哨兵模式、集群)概述及部署

    Redis(主从复制.哨兵模式.集群)概述及部署 前言 一.主从复制 (1)主从复制原理 (2)主从复制作用 (3)主从复制流程 (4)搭建主从复制 ①修改master节点配置文件 ②修改Slave节 ...

最新文章

  1. 在Java程序设计中,设置环境变量path和classpath的作用分别是什么?
  2. 加速 cinder 删除volume速度
  3. 企业级Nginx服务基础到架构优化详解--25条
  4. MAC安装mysql8.0.11以及修改root密码
  5. 安装配置RocketMQ,并配置Console
  6. excle快捷键(持续更新中)
  7. SQL Server 数据库身份认证以及包含数据库
  8. java当前时间长整数值_在Java中获取当前年份的整数值
  9. 苹果cmsv10精仿好看的挖片网免费自适应简约模板
  10. 论文笔记:Reciprocal Multi-Layer Subspace Learning for Multi-View Clustering
  11. office 64位安装
  12. 数据重生:让神经机器翻译中的不活跃样本“复活”
  13. apache、iis6、ii7独立ip主机屏蔽拦截蜘蛛抓取(适用vps云主机服务器)
  14. 真实性能大揭秘 热门移动显卡横向测试
  15. VideoPlayer视频播放
  16. 微信卡死代码 java_能让微信卡死的代码是什么 微信整人代码大全
  17. 项目经理成长的五个阶段
  18. MongoDB 学习笔记
  19. Cython 是什么?为什么会有 Cython?
  20. Android跟我一起来开发--微影之架构补充篇

热门文章

  1. BBSXP7 mssql版,从注入到拿WEBSHELL
  2. 批处理经典入门教程!!!
  3. 基于连通域字符分割的流程_基于连通域的快速文字图像分割算法
  4. DevExpress GridView 单元格修改标记
  5. 从零开始学USB(七、端点、管道、接口、配置、设备)【转】
  6. Linux终端设置成256色模式
  7. react hook监听窗口大小
  8. Java课程设计题目一:动物换位
  9. IBM的服务器芯片,IBM搞定5nm芯片工艺 可放置300亿晶体管
  10. C++ 异常处理(try catch)