Nginx安装部署和配置管理
发布日期:2021-05-08 00:58:36 浏览次数:41 分类:精选文章

本文共 8883 字,大约阅读时间需要 29 分钟。

Nginx安装部署和配置管理

1、nginx部署-Yum安装

访问nginx的官方网站:

Nginx版本类型

Mainline version: 主线版,即开发版

Stable version: 最新稳定版,生产环境上建议使用的版本

Legacy versions: 遗留的老版本的稳定版

在这里插入图片描述
Yum安装nginx

配置Yum源的官网:

1、配置nginx的Yum源

Installation instructions

Before you install nginx for the first time on a new machine, you need to set up the nginx packages repository. Afterward, you can install and update nginx from the repository.

安装说明

在新计算机上首次安装nginx之前,需要设置nginx软件包存储库。 之后,您可以从存储库安装和更新nginx。

RHEL/CENTOS

Install the prerequisites:

sudo yum install yum-utils -y
[nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=1
sudo yum install nginx -y

这里我们用稳定版本

[root@nginx-server yum.repos.d]# yum install -y nginx

[root@nginx-server yum.repos.d]# nginx -V //格式化打印

nginx version: nginx/1.16.0
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-server yum.repos.d]# nginx -v

nginx version: nginx/1.16.0

关闭防火墙和selinux:

[root@nginx-server ~]# getenforce

Enforcing

[root@nginx-server ~]# sed -i ‘/SELINUX/s/enforcing/disabled/’ /etc/selinux/config

[root@nginx-server ~]# systemctl stop firewalld

[root@nginx-server ~]# systemctl disable firewalld

启动并设置开机启动

[root@nginx-server ~]# systemctl start nginx

[root@nginx-server ~]# systemctl enable nginx

浏览器输入ip访问:

在这里插入图片描述

2、Nginx编译安装与配置使用

1、安装编译环境

yum -y install gcc gcc-c++

2、安装pcre软件包(使nginx支持http rewrite模块)

yum install -y pcre pcre-devel gd-devel

3、安装openssl-devel(使nginx支持ssl)

yum install -y openssl openssl-devel

4、安装zlib

yum install -y zlib zlib-devel

5、创建用户nginx

useradd nginx

passwd nginx

6、安装nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz[root@localhost ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/src[root@localhost ~]# cd /usr/local/nginx-1.16.0/[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream[root@localhost nginx-1.16.0]# make && make install
7、Nginx编译参数
# 查看 nginx 安装的模块[root@localhost ~]#/usr/local/nginx/sbin/nginx -V--prefix=/usr/local/nginx                        //指向安装目录--conf-path=/etc/nginx/nginx.conf                //指定配置文件--http-log-path=/var/log/nginx/access.log        //指定访问日志--error-log-path=/var/log/nginx/error.log        //指定错误日志--lock-path=/var/lock/nginx.lock                 //指定lock文件--pid-path=/run/nginx.pid                        //指定pid文件--http-client-body-temp-path=/var/lib/nginx/body    //设定http客户端请求临时文件路径--http-fastcgi-temp-path=/var/lib/nginx/fastcgi     //设定http fastcgi临时文件路径--http-proxy-temp-path=/var/lib/nginx/proxy         //设定http代理临时文件路径--http-scgi-temp-path=/var/lib/nginx/scgi           //设定http scgi临时文件路径--http-uwsgi-temp-path=/var/lib/nginx/uwsgi         //设定http uwsgi临时文件路径--with-debug                                        //启用debug日志--with-pcre-jit                                     //编译PCRE包含“just-in-time compilation”--with-ipv6                                         //启用ipv6支持--with-http_ssl_module                              //启用ssl支持--with-http_stub_status_module                      //获取nginx自上次启动以来的状态--with-http_realip_module                 //允许从请求标头更改客户端的IP地址值,默认为关--with-http_auth_request_module           //实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。--with-http_addition_module               //作为一个输出过滤器,支持不完全缓冲,分部分响应请求--with-http_dav_module                    //增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启--with-http_geoip_module                  //使用预编译的MaxMind数据库解析客户端IP地址,得到变量值--with-http_gunzip_module                 //它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。--with-http_gzip_static_module            //在线实时压缩输出数据流--with-http_image_filter_module           //传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到)--with-http_spdy_module                   //SPDY可以缩短网页的加载时间--with-http_sub_module                    //允许用一些其他文本替换nginx响应中的一些文本--with-http_xslt_module                   //过滤转换XML请求--with-mail                               //启用POP3/IMAP4/SMTP代理模块支持--with-mail_ssl_module                    //启用ngx_mail_ssl_module支持启用外部模块支持
8、修改配置文件/etc/nginx/nginx.conf
# 全局参数设置user nginx;  #设置nginx使用的用户#设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同,新版本支持 auto ,Nginx 将自动检测CPU核心数,并将worker process 数量设置成等同CPU核心数量worker_processes  auto;          error_log  logs/error.log;    #指定错误日志 worker_rlimit_nofile 1024;  #设置一个nginx进程能打开的最大文件数pid        /var/run/nginx.pid; events {        worker_connections  1024; #设置一个进程的最大并发连接数 }# http 服务相关设置 http {        include      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;    #设置访问日志的位置和格式     sendfile          on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载     # sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝    gzip              on;      #是否开启gzip压缩,将注释去掉开启     keepalive_timeout  65;     #设置长连接的超时时间# 虚拟服务器的相关设置     server {            listen      80;        #设置监听的端口         server_name  localhost;        #设置绑定的主机名、域名或ip地址         charset koi8-r;        # 设置编码字符         location / {                root  /var/www/nginx;           #设置服务器默认网站的根目录位置,需要手动创建            index  index.html index.htm;    #设置默认打开的文档             }         error_page  500 502 503 504  /50x.html; #设置错误信息返回页面         location = /50x.html {                root  html;        #这里的绝对位置是/usr/local/nginx/html        }     }  }
nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块。在http块中又包含http全局块、多个server块。每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。
9、检测nginx配置文件是否正确
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t[root@localhost ~]# mkdir -p /tmp/nginx
10、启动nginx服务
[root@localhost ~]# /usr/local/nginx/sbin/nginx
11、通过nginx命令控制nginx服务
nginx -c /path/nginx.conf  	     # 以特定目录下的配置文件启动nginx:nginx -s reload            	 	 # 修改配置后重新加载生效nginx -s stop  				 	 # 快速停止nginxnginx -s quit  				  	 # 完整有序的停止nginxnginx -t    					 # 测试当前配置文件是否正确nginx -t -c /path/to/nginx.conf  # 测试特定的nginx配置文件是否正确注意:nginx -s reload 命令加载修改后的配置文件,命令下达后发生如下事件1. Nginx的master进程检查配置文件的正确性,若是错误则返回错误信息,nginx继续采用原配置文件进行工作(因为worker未受到影响)2. Nginx启动新的worker进程,采用新的配置文件3. Nginx将新的请求分配新的worker进程4. Nginx等待以前的worker进程的全部请求已经都返回后,关闭相关worker进程5. 重复上面过程,知道全部旧的worker进程都被关闭掉
12、nginx 日志文件详解

​ nginx 日志文件分为 log_formataccess_log 两部分

​ log_format 定义记录的格式,其语法格式为

​ log_format 样式名称 样式详情

​ 配置文件中默认有

log_format  main  'shark= $remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';

在这里插入图片描述

在这里插入图片描述

3、使用 limit_rate 限制客户端传输数据的速度

1、编辑/etc/nginx/nginx.conf

location / {               root   /var/www/nginx/;            index  index.html index.htm;            limit_rate  2k;  #对每个连接的限速为2k/s        }

/usr/local/nginx/html/

2、重启服务

systemctl restart nginx

注意要点:

  • ​ 配置文件中的每个语句要以 ; 结尾
上一篇:Shell脚本-KVM创建新的虚拟机
下一篇:Mysql 数据库优化

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年05月09日 15时19分10秒