1. Nginx基本概述与部署

Posted yanmingbo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1. 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、mysqlphp

​ LNMT(linux、ngxin、mysql、tomcat)

1.

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配置文件

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

程序安装目录和路径

--http-client-body-temp-path=/var/cache/nginx/client_temp <br/>--http-proxy-temp-path=/var/cache/nginx/proxy_temp <br/>--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp <br/>--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp <br/>--http-scgi-temp-path=/var/cache/nginx/scgi_temp

临时缓存目录

--user=nginx<br/>--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进程支持最大连接数量默认1024
use 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 Nginx认识与基本使用 Nginx 实现反向代理配置负载均衡

Nginx认识与基本使用 Nginx 实现反向代理配置负载均衡

Nginx认识与基本使用 Nginx 实现反向代理配置负载均衡

Ansible-基本概述

nginx基本概述

用docker启动nginx影响性能吗