Nginx安装和配置(yum安装和编译安装)

1. Yum 安装 Nginx

nginx的官方网站:http://www.nginx.org
Nginx版本类型
Mainline version:主线版,即开发版
Stable version:最新稳定版,生产环境上建议使用的版本
Legacy versions:遗留的老版本的稳定版
1
Yum安装nginx
配置Yum源的官网:http://nginx.org/en/linux_packages.html

1.1. 配置 Nginx 的 Yum 源

安装说明:
在新计算机上首次安装nginx之前,需要设置nginx软件包存储库。 之后,您可以从存储库安装和更新nginx。
要设置yum存储库,请创建名为/etc/yum.repos.d/nginx.repo的文件,其中包含以下内容:

//安装yum工具包
[root@localhost ~]# yum -y install yum-utils
[root@localhost ~]# 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

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

1.2. 安装 Nginx

这里我们用稳定版本

[root@localhost ~]# yum install -y nginx
[root@localhost ~]# nginx -V    //查看nginx的详细信息,包括模块,安装目录、用户等
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (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@localhost ~]# nginx -v   //查看版本号
nginx version: nginx/1.24.0

//关闭防火墙和selinux
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
​
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld

//启动并设置开机启动
[root@localhost ~]# systemctl start nginx 
[root@localhost ~]# systemctl enable nginx 

浏览器输入ip访问:
2

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

2.1. 安装编译环境

[root@localhost ~]# yum -y install gcc gcc-c++ pcre pcre-devel gd-devel openssl openssl-devel  zlib zlib-devel

2.2. 创建用户nginx

[root@localhost ~]# useradd nginx && echo "nginx" | passwd --stdin nginx

2.3. 安装 Nginx

[root@localhost ~]# rpm -qa|grep wget && wget http://nginx.org/download/nginx-1.24.0.tar.gz
[root@localhost ~]# mkdir -p /tmp/nginx/
[root@localhost ~]# tar -xzvf nginx-1.24.0.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.24.0/
[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/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.24.0]# make && make install

2.4. Nginx 编译参数详解

--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文件(进程锁文件路径)Nginx进程锁(lock file)是用于控制Nginx进程并发性的一种机制。在Nginx启动时,会创建一个进程锁文件,用于记录Nginx主进程的PID,并避免多个Nginx进程同时运行。
--pid-path=/run/nginx.pid                        ##指定pid文件
--http-client-body-temp-path=/var/lib/nginx/body    ##设定http客户端请求临时文件路径(客户端请求体临时文件路径)
#作用:HTTP客户端请求体临时文件用于存储客户端请求体的临时数据,通常情况下,当客户端上传的数据比较大时,Nginx会将请求体存储在临时文件中,以避免内存不足的问题
​
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi     ##设定http fastcgi临时文件路径(用于设置FastCGI临时文件路径)
#FastCGI是一种Web服务器和应用程序之间通信的协议,它可以用于将动态生成的Web页面的生成过程交给独立的FastCGI进程处理,从而提高Web服务器的性能和可靠性。在处理FastCGI请求时,Nginx会使用FastCGI临时文件来存储和传递FastCGI进程生成的数据。
​
--http-proxy-temp-path=/var/lib/nginx/proxy         ##设定http代理临时文件路径(用于设置代理模块临时文件路径)
#代理模块是Nginx中的一个核心模块,用于将客户端的请求转发到后端服务器,并将后端服务器的响应返回给客户端。在处理代理请求时,Nginx会使用代理模块临时文件来存储和传递代理请求的数据。
​
--http-scgi-temp-path=/var/lib/nginx/scgi           ##设定http scgi模块临时路径(用于设置SCGI(Simple Common Gateway Interface)模块临时文件路径)
#SCGI是一种Web服务器和应用程序之间通信的协议,类似于FastCGI。在处理SCGI请求时,Nginx会使用SCGI模块临时文件来存储和传递SCGI进程生成的数据。
​
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi         ##设定http uwsgi临时文件路径(用于设置uWSGI(Universal Web Server Gateway Interface)模块临时文件路径)
#uWSGI是一种Web服务器和应用程序之间通信的协议,类似于FastCGI和SCGI。在处理uWSGI请求时,Nginx会使用uWSGI模块临时文件来存储和传递uWSGI进程生成的数据。
​
--with-debug        #启用调试日志信息,用于调试和排查问题。
--with-ipv6         #启用IPv6支持。
--with-http_ssl_module  #启用HTTPS协议支持的SSL/TLS模块。
​
--with-http_stub_status_module      #启用Nginx自上次启动以来的状态信息模块,可以通过HTTP接口获取。
--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方法,用于WebDAV协议支持,默认为关闭状态,需要编译开启。
​--with-http_geoip_module            #使用预编译的MaxMind数据库解析客户端IP地址,得到变量值。
​--with-http_gunzip_module           #用于解压具有“Content-Encoding: gzip”头的响应,以支持不支持“gzip”编码方法的客户端。
​--with-http_gzip_static_module      #在线实时压缩输出数据流,以降低网络带宽消耗。
​--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支持,用于支持SSL/TLS加密的SMTP/POP3/IMAP4协议。#这些选项可以根据具体需求进行选择和配置,以满足不同的应用场景。需要注意的是,一些模块默认是关闭的,需要通过编译时的选项进行开启。同时,这些选项需要在编译Nginx之前进行设置。--with    表示在编译过程中需要给nginx添加的模块
--without 表示编译nginx时默认该模块是添加进去的当使用这个参数时表示将默认编译的模块移除
//查看 nginx 安装的模块
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/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 ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.24.0

2.5. 修改配置文件/etc/nginx/nginx.conf

[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost nginx]# cp nginx.conf{,.bak}
[root@localhost nginx]# vim nginx.conf
# 全局参数设置
user nginx;  #设置nginx使用的用户
worker_processes  4;          #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同 
error_log  logs/error.log;    #指定错误日志 
pid        /var/run/nginx.pid; 
events { 
    worker_connections  1024; #设置一个进程的最大并发连接数 
}# http 服务相关设置 
http { 
    include      mime.types; #关联mime类型,关联资源的媒体类型
    default_type  application/octet-stream; #根据文件的后缀来匹配相应的MIME类型
    log_format  main  'remote_addr - remote_user [time_local] "request" '
                      'status body_bytes_sent "$http_referer" '
                      '"http_user_agent" "http_x_forwarded_for"'; 
   
    #这一段是Nginx的日志格式配置,这一小段忽略:
    remote_addr:客户端IP地址。
    remote_user:客户端用户名。
    [time_local]:访问时间,格式为本地时间。
    "request":HTTP请求方法、URI和协议版本。
    status:HTTP响应状态码。
    body_bytes_sent:发送给客户端的数据量,不包括响应头。
    "$http_referer":HTTP Referer头信息,表示访问来源。
    "http_user_agent":HTTP User-Agent头信息,表示客户端浏览器或应用程序的信息。
    "http_x_forwarded_for":HTTP X-Forwarded-For头信息,表示客户端真实IP地址,用于反向代理场景。
    
    
    access_log  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式 
    sendfile          on;      # 用于开启文件高效传输模式,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
    tcp_nopush        on;      # 减少网络报文段数量,当有数据时,先别着急发送,确保数据包已经装满数据,避免了网络拥塞
    tcp_nodelay       on;      # 提高I/O性能,禁用Nagle算法,确保数据尽快发送,提高可数据传输效率 (Nagle算法是一种常用的TCP网络传输优化算法,它将多个小的数据包合并成一个大的数据包发送,以减少网络传输的开销)
    
    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
        } 
    } 
 }

1、nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块。在http块中又包含http全局块、多个server块。
每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。

2、Nginx的组成:

  • nginx的二进制可执行文件:用来启动关闭nginx的,由各个模块编译出来的
    #—/usr/local/nginx/sbin/nginx
  • nginx.conf配置文件:控制nginx
  • access.log访问日志:记录每条http请求
  • error.log 错误日志:定位问题

2.6. 检测 Nginx 配置文件是否正确

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -t
​
//最简单的方法让服务器全局都能使用nginx命令
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx  /usr/sbin/nginx  #注意一定要是绝对路径!!!
​
[root@localhost nginx]# nginx  -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2.7. 启动 Nginx 服务

[root@localhost ~]# /usr/local/nginx/sbin/nginx    
//或者直接 nginx

2.8. 通过 Nginx 命令控制 Nginx 服务

nginx -c /path/nginx.conf  -     # 以特定目录下的配置文件启动nginx
nginx -s reload                  # 修改配置后重新加载生效
nginx -s stop                    # 快速停止nginx
nginx -s quit                    # 正常停止nginx
nginx -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进程都被关闭掉

3. Nginx 日志文件详解

  • nginx 日志文件分为 log_formataccess_log 两部分
  • log_format 定义记录的格式,其语法格式为:# log_format 样式名称 样式详情

配置文件中默认有,编译安装的nginx默认是关闭该格式的

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

参数解释

变量说明
remote_addr客户端IP地址
remote_user客户端用户名
[time_local]访问时间,格式为本地时间
“request”HTTP请求方法、URI和协议版本
statusHTTP响应状态码
body_bytes_sent发送给客户端的数据量,不包括响应头
“$http_referer”HTTP Referer头信息,表示访问来源
“http_user_agent”HTTP User-Agent头信息,表示客户端浏览器或应用程序的信息
“http_x_forwarded_for”HTTP X-Forwarded-For头信息,表示客户端真实IP地址,用于反向代理场景
  • 40
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TA548464

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值