Linux之Nginx入门

一、Nginx介绍

Nginx是一个开源且高性能、可靠的http web服务、代理服务。

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

  • 高性能,高并发 Nginx支持很高的并发,Nginx在处理大量并发的情况下比其他web服务要快

  • 轻量且高扩展性
  • 轻量:功能模块少,只保留核心模块,其他代码模块化 (易读,便于二次开发,对于开发人员非常友好)
  • 高扩展性:需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块
  • 高可靠性 只要不过分几乎不会出现问题,其他的web服务需要每隔一段时间进行重启,Nginx不需要;Nginx的宕机时间,是99999级别
  • 支持热部署 Nginx可以在运行期间,更新迭代,代码部署。
  • 大多公司都在用Nginx
  • Nginx技术成熟,具备的功能是企业最常使用而且最需要的;
  • 适合当前主流架构趋势, 微服务、云架构、中间层;
  • 统一技术栈, 降低维护成本, 降低技术更新成本;
  • Nginx使用的是Epoll网络模型
  • Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
  • Epoll: 当用户发起一次请求,epoll模型会直接进行处理,效率高效,并无连接限制。
  • Nginx应用场景 Linux之Nginx入门

Tomcat
Resin
weblogic
Jboss

二、Nginx安装

1.epol源安装       web01
2.官方源安装       web03
3.源码包安装       web03

#三种安装方式都需要安装nginx运行的依赖环境
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
[root@web01 ~]# yum install -y nginx

配置官方源:

#配置官方源:
[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

#下载:
[root@web02 ~]# yum install -y nginx

三、Nginx命令

nginx #启动nginx。 等价于systemctl start nginx

nginx -s reopen #重启Nginx。   等价于systemctl restart nginx

nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx。 等价于systemctl reload nginx

nginx -s stop #强制停止Nginx服务。 等价于systemctl stop nginx

nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)

nginx -t #检测配置文件是否有语法错误,然后退出

nginx -?,-h #打开帮助信息

nginx -v #显示版本号信息并退出

nginx -V #显示版本号和配置选项信息,然后退出

nginx -V 2>&1 | sed "s/\s\+--/\n --/g" #模块分行输出,格式化输出

killall nginx #杀死所有nginx进程

systemctl enable nginx  #加入开机自启动
    Centos6:
        启动:nginx
            service nginx start
            /etc/init.d/nginx start
        加入开机自启:
            chkconfig nginx on

nginx -T #检测配置文件是否有语法错误,转储并退出

nginx -q #在检测配置文件期间屏蔽非错误信息(打印错误日志)

nginx -p prefix #设置nginx的工作目录(默认是:/usr/share/nginx/)

nginx -c filename #设置配置文件路径(默认是:/etc/nginx/nginx.conf)

nginx -g directives #设置配置文件外的全局指令

四、Nginx服务平滑无感知添加模块和升级

[root@web02 ~]# nginx -V        #显示版本和配置选项信息,然后退出
nginx version: nginx/1.20.2
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

############################内容解析#######################################

nginx version: nginx/1.20.2                 #nginx版本
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            #nginx文件的安装路径,其它选项如果使用相对路径,那么以此路径为基础路径
 --sbin-path=/usr/sbin/nginx    #二进制程序的安装目录
 --modules-path=/usr/lib64/nginx/modules    #模块安装路径
 --conf-path=/etc/nginx/nginx.conf  #设置nginx的conf文件路径
 --error-log-path=/var/log/nginx/error.log  #设置nginx错误日志路径
 --http-log-path=/var/log/nginx/access.log  #设置nginx的访问日志路径
 --pid-path=/var/run/nginx.pid  #设置nginx的pid文件路径
 --lock-path=/var/run/nginx.lock    #设置lock文件临时存放的路径
 --http-client-body-temp-path=/var/cache/nginx/client_temp  #设置http客户端主体临时缓存路径
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp     #设置http反向代理临时路径
 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp     #设置http的fastcgi临时缓存路径
 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp #设置uwsgi临时目录
 --http-scgi-temp-path=/var/cache/nginx/scgi_temp   #scgi临时存放目录
 --user=nginx   #设置启动 worker 进程时所使用的非特权用户名
 --group=nginx  #设置启动 worker 进程时所使用的非特权用户组名
 --with-compat
 --with-file-aio
 --with-threads
    ......

1.安装依赖
[root@web02 nginx-1.20.2]# yum install -y openssl openssl-devel

2.再生成一次
[root@web02 nginx-1.20.2]# ./configure --prefix=/usr/local/nginx-1.20.2-new --user=www --group=www --without-http_gzip_module --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'

3.安装
[root@web02 nginx-1.20.2]# make && make install

4.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.20.2-new /usr/local/nginx

5.重启服务
[root@web02 nginx-1.20.2]# systemctl restart nginx
1.下载新版本的包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz

2.解压
[root@web02 ~]# tar xf nginx-1.20.2.tar.gz

3.生成
[root@web02 nginx-1.20.2]# cd nginx-1.20.2
[root@web02 nginx-1.20.2]# ./configure --prefix=/usr/local/nginx-1.20.2 --user=www --group=www --without-http_gzip_module --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

4.编译安装
[root@web02 nginx-1.20.2]# make && make install

5.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.20.2 /usr/local/nginx

6.重启服务
[root@web02 ~]# systemctl restart nginx

五、Nginx相关文件

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

/etc/nginx/nginx.conf 配置文件 nginx主配置文件 /etc/nginx/conf.d/default.conf 配置文件 默认网站配置文件

/etc/nginx/fastcgi_params 配置文件 Fastcgi代理配置文件 /etc/nginx/scgi_params 配置文件 scgi代理配置文件 /etc/nginx/uwsgi_params 配置文件 uwsgi代理配置文件

/etc/nginx/win-utf 配置文件 Nginx编码转换映射文件 /etc/nginx/koi-utf 配置文件 Nginx编码转换映射文件 /etc/nginx/koi-win 配置文件 Nginx编码转换映射文件 /etc/nginx/mime.types 配置文件 Content-Type与扩展名

/usr/sbin/nginx 命令 Nginx命令行管理终端工具 /usr/sbin/nginx-debug 命令 Nginx命令行与终端调试工具

/var/log/nginx 目录 Nginx默认存放日志目录 /etc/logrotate.d/nginx 配置文件 Nginx默认的日志切割

六、Nginx配置文件

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

Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)

配置文件内容:

[root@web01 ~]# cat /etc/nginx/nginx.conf
#########################核心模块####################
#指定启动的用户
user  www;
#nginx的worker进程的数量,auto==CPU数量
worker_processes  auto;
#指定错误日志存放的路径以及记录的级别 debug/info/notice/warn/error/emerg
error_log  /var/log/nginx/error.log notice;
#指定pid文件
pid        /var/run/nginx.pid;

########################事件驱动模块#################
events {
    #每个worker工作进程的最大连接数
    worker_connections  1024;
}

######################http内核模块###################
http {
    #包含,nginx可识别的文件类型
    include       /etc/nginx/mime.types;
    #当nginx不识别文件类型的时候,默认下载
    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;
    #高效传输
    #tcp_nopush     on;
    #开启长连接
    keepalive_timeout  65;
    #开启压缩
    #gzip  on;
    #包含网站的配置文件
    include /etc/nginx/conf.d/*.conf;

    #一个server表示一个网站
    server {
        #监听端口
        listen       80;
        #网站提供的域名
        server_name  localhost;
        #字符集
        charset utf8;
        #匹配、控制访问的网站站点
        location / {
            #指定站点目录
            root   /usr/share/nginx/html;
            #指定默认访问的页面
            index  index.html index.htm;
        }
    }
}
access_log /var/log/nginx/access.log json ;

日志格式可以用下面的json格式,更加清晰明了:

log_format json '{"@timestamp":"$time_iso8601",'
          '"host":"$server_addr",'
          '"service":"nginxTest",'
          '"trace":"$upstream_http_ctx_transaction_id",'
          '"log":"log",'
          '"clientip":"$remote_addr",'
          '"remote_user":"$remote_user",'
          '"request":"$request",'
          '"http_user_agent":"$http_user_agent",'
          '"size":$body_bytes_sent,'
          '"responsetime":$request_time,'
          '"upstreamtime":"$upstream_response_time",'
          '"upstreamhost":"$upstream_addr",'
          '"http_host":"$host",'
          '"url":"$uri",'
          '"domain":"$host",'
          '"xff":"$http_x_forwarded_for",'
          '"referer":"$http_referer",'
          '"status":"$status"}';

七、搭建小游戏案例

[root@web01 ~]# vim /etc/nginx/conf.d/game.conf
server {
    listen 80;
    server_name www.mario.com;
    #server_name localhost;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}
[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
[root@web01 ~]# mkdir -p /opt/Super_Marie
[root@web01 ~]# cd /opt/Super_Marie
[root@web01 Super_Marie]# rz
[root@web01 Super_Marie]# unzip mario.zip
[root@web01 Super_Marie]# systemctl restart nginx
Windows中hosts文件所在位置:
C:\Windows\System32\drivers\etc\hosts

172.16.1.7 www.mario.com

八、再搭建一个游戏

[root@web01 ~]# vim /etc/nginx/conf.d/gametwo.conf
server {
    listen 80;
    server_name www.tank.com;

    location / {
        root /opt/tank;
        index index.html;
    }
}

[root@web01 opt]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web01 ~]# mkdir -p /opt/tank

[root@web01 opt]# systemctl restart nginx
[root@web01 ~]# cd /opt/tank
[root@web01 tank]# rz
[root@web01 tank]# unzip tankedazhan.zip
C:\Windows\System32\drivers\etc\hosts

172.16.1.7 www.mario.com www.tank.com

Original: https://www.cnblogs.com/JZjuechen/p/15760844.html
Author: JZEason
Title: Linux之Nginx入门

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524210/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • linux挂载文件系统

    当使用光驱等硬件设备时,必须将其挂载到系统中,只有这样Linux才能识别。 1、给虚拟机中centos7系统添加一块新的硬盘,添加以后必须重启centos7才能生效 2、在root…

    Linux 2023年5月27日
    0103
  • 从Windows备份文件到Linux服务器

    各种深坑 尝试1—FTP 因为组内有FTP服务器,所以尝试用bat编写把文件压缩后放到FTP上。其中压缩使用7zip.exe来完成,定时任务使用windows自带的定时任务。 整个…

    Linux 2023年6月8日
    0127
  • chmod 报错 changing permissions of ‘xxx’: Operation not permitted

    chmod的底层实现是 chattr命令,用此命的功能更为强大,甚至可以锁定文件,即使root用户也操作不了此文件。 先使用lsattr查看文件属性 `bashlsattr tem…

    Linux 2023年6月13日
    0105
  • 网络扫描(一)

    使用工具:Kali Linux、Metaspoliatable(作为攻击目标) 扫描的4个不同阶段 用ping验证系统是否正在运行。 用Nmap扫描目标主机的端口。 用Nmap脚本…

    Linux 2023年6月14日
    0120
  • redis八种基本数据类型及其应用

    NoSQL 开发中或多或少都会用到,也是面试必问知识点。最近这几天的面试每一场都问到了。但是感觉回答的并不好,还有很多需要梳理的知识点。这里通过几篇 Redis 笔记整个梳理一遍,…

    Linux 2023年5月28日
    092
  • Tomcat启动乱码

    1、找到安装的tomcat的conf目录2、找到logging.properties配置文件3、在文件中找到 java.util.logging.ConsoleHandler.en…

    Linux 2023年6月7日
    0107
  • 019 Linux tcpdump 抓包案例入门可真简单啊?

    1 tcpdump 是什么? 2 tcpdump 常用命令参数 3 tcpdump 抓包wss,配合Wireshark分析 4 tcpdump 抓包白度,配合Wireshark分析…

    Linux 2023年5月27日
    086
  • cobbler

    cobbler 1. cobbler简介 2. cobbler服务端部署 cobbler简介 Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速…

    Linux 2023年6月7日
    085
  • 小记:音频格式转化ByPython(上)

    近日新买了个耳机,店家附送了一些周董的无损音乐资源,收到货后迫不及待的下载试听,才发现这些资源是wav格式的,导入播放器后歌名、作者、专辑等全是未知,当时想着是不是店家的资源有问题…

    Linux 2023年6月8日
    0115
  • 聊聊Mysql主从同步读写分离配置实现

    Hi,各位热爱技术的小伙伴您们好,好久没有写点东西了,今天写点关于mysql主从同步配置的操作日志同大家一起分享。最近自己在全新搭建一个mysql主从同步读写分离数据库简单集群,我…

    Linux 2023年6月14日
    098
  • C盘空间怎么不够了?原来杀毒软件隔离区太大了

    打开小红伞的隔离区,文件全选,删除,居然一下多出20个G. C盘空间怎么不够了?原来杀毒软件隔离区太大了 麻了。打开小红伞的隔离区,文件全选,删除,一下多出20个G. 还有一个比较…

    Linux 2023年6月6日
    0118
  • 5.4 Linux Vim基本操作

    《Vim三种工作模式》一节给大家详细介绍了 Vim 的 3 种工作模式,本节来学习如何使用 Vim 编辑文件。 首先学习如何使用 Vim 打开文件。 Vim 打开文件 使用 Vim…

    Linux 2023年6月7日
    0106
  • vim的使用

    1、概述: Vim 是从 vi 发展出来的一个文本编辑器。具有代码补全、编译及错误跳转等功能 2、vim编辑器的常用命令: 图源:https://vimsky.com/articl…

    Linux 2023年5月27日
    0132
  • 离线版centos8安装docker笔记

    嗨嗨哈哈,已经很久没有坐下来胡编乱造一点笔记了,平时云服务器搞惯了,一个命令就安装好了docker了的,但这次生不逢时的新机房就没那么幸运了,有多不逢时超乎想象,不仅仅服务器没有外…

    Linux 2023年5月27日
    0136
  • 记一次burp suite文件上传漏洞实验

    一·文件上传漏洞概念文件上传漏洞是指 Web 服务器允许用户在没有充分验证文件名称、类型、内容或大小等内容的情况下将文件上传到其文件系统。未能正确执行这些限制可能意味着即使是基本的…

    Linux 2023年6月7日
    0108
  • Windows 11 绕过 TPM 方法

    在 Windows 11 安装界面按 Shift + F10 打开命令行界面,执行如下命令: REG ADD HKLM\SYSTEM\Setup\LabConfig /v Bypa…

    Linux 2023年6月13日
    0109
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球