CentOS7 源码安装Nginx及Nginx基本管理设置

CentOS7 安装 参考文档

CentOS7最小安装后初始化安装工具

1:yum install net-tools 参考文档

2:源码安装wget 参考文档 或者执行 yum -y install wget

CentOS7 源码安装包步骤

一:Nginx 安装步骤

1-1:源码下载地址 http://nginx.org/en/download.html

执行命令:wget wget http://nginx.org/download/nginx-1.21.3.tar.gz

1-2:解压下载的文件

执行命令:tar -zxvf {压缩文件名}.tar.gz

1-3:配置configure 检测安装及依赖信息

查看命令文档执行:./configure –help

CentOS7 源码安装Nginx及Nginx基本管理设置

先安装编译器

执行:sudo yum -y install gcc

再次执行:./configure 进行配置 如下图:

CentOS7 源码安装Nginx及Nginx基本管理设置

检测到有依赖包没有装,根据提示去安装 pcre

执行命令:sudo yum -y install pcre-devel

再次执行:./configure 如下图:

CentOS7 源码安装Nginx及Nginx基本管理设置

根据提示继续安装依赖包:

执行命令:sudo yum -y install zlib-devel

一直重复执行./configure 直到检测到没有依赖项就说明检测通过了,如下图:

CentOS7 源码安装Nginx及Nginx基本管理设置

检测并指定安装路径命令:./configure –prefix=/usr/local/nginx

CentOS7 源码安装Nginx及Nginx基本管理设置

1-4:执行make指令:源码生成可执行文件,中途不能出现error

CentOS7 源码安装Nginx及Nginx基本管理设置

1-5:安装 make install

执行安装命令:sudo make install

CentOS7 源码安装Nginx及Nginx基本管理设置

测试:

1:进入:/usr/local/nginx 执行:./sbin/nginx

2:在浏览器中输入:http://localhost

3:安装一个文本浏览器测试 yum -y install elinks

CentOS7 源码安装Nginx及Nginx基本管理设置

Nginx相关目录文件

nginx path prefix:/usr/local/nginx (Nginx安装目录)

nginx binary file:/usr/local/nginx/sbin/nginx(Nginx启动目录)

nginx moduless path:/usr/local/nginx/modules(Nginx相关模块目录)

nginx configuration prefix:/usr/local/nginx/conf(Nginx配置文件目录)

nginx configuration file:/usr/local/nginx/conf/nginx.con(nginx启动配置文件)

nginx pid file:/usr/local/nginx/logs/nginx.pid(nginx进程号)

nginx error log file:/usr/local/nginx/logs/error.log:(nginx错误日志文件)

nginx http access log file:/usr/local/nginx/logs/access.log(访问日志文件)

先关闭防火墙 (测试需要在局域网中访问)

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

Nginx添加到环境变量

使用软连接将 nginx链接到 /usr/local/sbin

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin || /usr/local/sbin/ | grep “nginx”

CentOS7 源码安装Nginx及Nginx基本管理设置

显示当前环境变量 PATH

echo $PATH

编辑 .bash_profile文件

vim ~/.bash_profile

.bash_profile文件末尾加入以下内容

export PATH=$PATH:/usr/local/nginx/sbin

CentOS7 源码安装Nginx及Nginx基本管理设置

引用.bash_profile文件

source ~/.bash_profile

使用nginx命令

启动nginx

nginx

停止nginx

nginx -s quit

nginx部分配置

CentOS7 源码安装Nginx及Nginx基本管理设置CentOS7 源码安装Nginx及Nginx基本管理设置
# 启动该程序的默认程序
#user  nobody;
# 一个主进程和多个工作进程。这里定义的是主进程数量
worker_processes  4;

# 全局错误日志的位置及日志格式
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/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  logs/access.log  main; # 全局日志路径
    # $remote_addr与$http_x_forwarded_for用以记录客户端ip地址
    # $remote_user:记录客户端名称
    # $time_local:记录访问时间与时区
    # $request:记录访问状态 200成功
    # $body_bytes_sent:记录发送给客户端文件主体内容大小
    # $http_referer:记录从哪个页面链接访问过来的
    # $http_user_agent:记录客户端浏览器相关信息

    # sendfie指令指定nginx是否调用sendfile函数(zero copy 方式)来输出文件
    sendfile        on;
    # 允许或禁止使用socke的TCP_CORK的选项仅在使用sendfile的时候使用
    #tcp_nopush     on;
    # 长连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    # 开启压缩
    #gzip  on;
    # 默认网站  配置虚拟主机
    server {
    # 虚拟主机使用的端口
        listen       80;
    # 主机域名
        server_name  localhost;
    # 支持的字符集
        charset utf-8;
        #访问日志路径
        #access_log  logs/host.access.log  main;
    # 定义web根路径
        location / {
        # 根目录路径
            root   html;
        # 索引页面
            index  index.html index.htm;
        }

    # 访问控制目录
    location /a {
        # 允许访问
        allow  127.0.0.0;
        # 不允许
        deny  all;
        # return 404;
        return  http://www.baidu.com;
    }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        # 根据错误码返回对应的页面
        error_page   500 502 503 504  /50x.html;
        #
    location = /50x.html {
            root   html;
        }

    }
}

View Code

如果修改了配置文件先进入nginx目录中:cd /usr/local/nginx ,在执行:./sbin/nginx -g ./conf/nginx.conf 检测配置文件是否正确

修改配置重启nginx 方式一:

先执行:killall nginx

在执行:cd /usr/local/nginx 执行:./sbin/nginx

修改配置重启nginx 方式一:

killall -s HUP nginx (重新加载配置文件)

nginx访问目录配置

注意:访问控制目录中的location /a{} 这里的location值得就是 web根路径中的root路径

# 定义web根路径
        location / {
        # 根目录路径
            root   html;
        # 索引页面
            index  index.html index.htm;
        }

    # 访问控制目录
    location /a {
        # 允许访问
        allow  127.0.0.0;
        # 不允许
        deny  all;
        # return 404;
        return  http://www.baidu.com;
    }

Nginx日志管理

Nginx访问日志主要有两个参数控制

1:log_format 用来定义日志格式,可以定义多种日志格式,取不同名字即可

2:access_log 用来指定日志文件路径及使用何种日志格式记录日志

log_format格式变量:

access_log logs/access.log main; # 全局日志路径

$remote_addr与$http_x_forwarded_for用以记录客户端ip地址

` # $remote_user:记录客户端名称

$time_local:记录访问时间与时区

$request:记录访问状态 200成功

$body_bytes_sent:记录发送给客户端文件主体内容大小

$http_referer:记录从哪个页面链接访问过来的

$http_user_agent:记录客户端浏览器相关信息

CentOS7 源码安装Nginx及Nginx基本管理设置

自定义格式为json格式:

# 自定义日志格式
    # log_format default_fmt '[$time_local] $remote_addr "$request"'
    # 自定义json日志格式
    log_format default_fmt_json '{"@timestamp":"$time_local",'
                                '"client_ip":"$remote_addr",'
                                '"request":"$request",'
                                '"status":"$status",'
                                '"bytes":"$body_bytes_sent",'
                                '"x_forwarded":"$http_x_forwarded_for",'
                                '"referer":"$http_referer"'
                                '}';

两种格式测试效果如下:

CentOS7 源码安装Nginx及Nginx基本管理设置

Nginx图片防盗链

# 防盗链(只针对b目录)
        # location /b {
        # 项目中以下文件后缀
        location ~* \.(png|gif|bmp|jpg|jpeg)$ {
        valid_referers none blocked *.ticp.net;
        if ($invalid_referer){
                return 403;
        }
        }

Nginx虚拟主机

一个web服务器软件默认情况下只能发布一个web,因为一个web分享出去需要三个条件(IP、Port、Domain name)

虚拟主机就是把一台物理服务器划分成多个虚拟服务器,每个虚拟主机都可以有独立的域名和独立的目录。

[En]

Virtual host is to divide a physical server into multiple virtual servers, and each virtual host can have an independent domain name and an independent directory.

CentOS7 源码安装Nginx及Nginx基本管理设置

1:基于IP的虚拟主机

准备:

1:在nginx根目录下准备两个网站如:

/usr/local/nginx/html/web1/index.html

/usr/local/nginx/html/web1/index.html

2:添加一个子IP,使用逻辑网卡添加一个 自网卡的方式

添加网卡:ifconfig ens33:1 192.168.0.131/24 up

删除网卡:ifconfig ens33:1 down

CentOS7 源码安装Nginx及Nginx基本管理设置

基于IP虚拟主机nginx配置信息 (/usr/local/nginx/conf/nginx.conf)

user centos;
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format default_fmt_json '{"@timestamp":"$time_local",'
                    '"client_ip":"$remote_addr",'
                '"request":"$request",'
                '"status":"$status",'
                '"bytes":"$body_bytes_sent",'
                '"x_forwarded":"$http_x_forwarded_for",'
                '"referer":"$http_referer"'
                '}';
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       192.168.0.130:80;
        server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            root   html/web1;
            index  index.html index.htm;
        }
    }
    server {
        listen       192.168.0.131:80;
        server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
}

重启nginx 测试 (先:killall nginx 在:../sbin/nginx )

CentOS7 源码安装Nginx及Nginx基本管理设置

CentOS7 源码安装Nginx及Nginx基本管理设置

2:基于端口的虚拟主机(只需要修改配置即可)

CentOS7 源码安装Nginx及Nginx基本管理设置CentOS7 源码安装Nginx及Nginx基本管理设置
user centos;
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format default_fmt_json '{"@timestamp":"$time_local",'
                    '"client_ip":"$remote_addr",'
                '"request":"$request",'
                '"status":"$status",'
                '"bytes":"$body_bytes_sent",'
                '"x_forwarded":"$http_x_forwarded_for",'
                '"referer":"$http_referer"'
                '}';
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        #server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            root   html/web1;
            index  index.html index.htm;
        }
    }
    server {
        listen       8080;
        #server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
}

View Code

CentOS7 源码安装Nginx及Nginx基本管理设置

测试

CentOS7 源码安装Nginx及Nginx基本管理设置

3:基于域名的虚拟主机配置文件

CentOS7 源码安装Nginx及Nginx基本管理设置

Nginx反向代理

代理服务器,客户在发送请求的时候,不会直接发送给目的主机,而是先发送给代理服务器,代理服务器接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器硬盘中,在发送给客户机

[En]

Proxy server, when sending a request, the client will not send it directly to the destination host, but first send it to the proxy server. after accepting the client request, the proxy server sends it to the host and receives the data returned by the destination host. It is stored in the hard disk of the proxy server and sent to the client.

CentOS7 源码安装Nginx及Nginx基本管理设置

应用场景:

堡垒机场景

内网服务器发布场景

缓存场景

CentOS7 源码安装Nginx及Nginx基本管理设置

代理服务器原理:

CentOS7 源码安装Nginx及Nginx基本管理设置

代理配置文件

CentOS7 源码安装Nginx及Nginx基本管理设置CentOS7 源码安装Nginx及Nginx基本管理设置
user centos;
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format default_fmt_json '{"@timestamp":"$time_local",'
                    '"client_ip":"$remote_addr",'
                '"request":"$request",'
                '"status":"$status",'
                '"bytes":"$body_bytes_sent",'
                '"x_forwarded":"$http_x_forwarded_for",'
                '"referer":"$http_referer"'
                '}';
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        #server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            proxy_pass http://wendj.ticp.net;
        }
    }
}

View Code

CentOS7 源码安装Nginx及Nginx基本管理设置

Nginx限速

Nginx官方版本限制IP的链接和并发分别有两个模块

limit_req_zone:用来限制单位时间内的请求数,即速率限制。

limit_req_conn:用来限制同一时间连接数,即并发限制。

CentOS7 源码安装Nginx及Nginx基本管理设置

Nginx中URL重写

rewrite模块(ngx_http_rewrite_module)

Rewrite功能是Nginx服务器提供的一个重要功能。几乎所有的web产品必备技能,用于实现URL重写。URL重写是非常常用的功能,比如它可以在我们改变网站结构后,不需要客户端修改原来的书签,也不需要其它网站吸怪对我网站的友情链接,还可以在一定程度上提高网站安全性。

Nginx服务器Rewrite功能是依赖于PCRE(PerlCompatibleRegularExpression)

Nginx优化

Nginx是主进程+工作进程模型

worker_processes 1:工作进程数量,按CPU的总核心调整

worker_cpu_affinity 0001 0100 1000:CPU的亲和力

worker_connections 1024:一个工作进程的并发数

查看CPU核数指令:cat /proc/cpuinfo |grep “flags”|wc -l

CentOS7 源码安装Nginx及Nginx基本管理设置

统计nginx连接数:netstat -antpl|grep nginx|grep ESTABLISHED|wc -l

Nginx长连接

http协议属于TCP协议

优化目标:减少三次握手和四次挥手次数

keepalive_timeout 5:长连接时间

keepalive_requests 8192:每个长连接接受最大请求数

Nginx数据压缩

gzip on; # 开启压缩

gzip_proxied any; # 任何时候都压缩

gzip_min_length 1k; # 启用压缩最小文件,小于设置的不会被压缩

gzip_buffers 4 8k; # 压缩的缓存区大小

gzip_comp_level 6; # 压缩级别1-9数字越大压缩的越好,也越占CPU时间

gzip_types text/plain text/css application/x-javascript application/javascript application/xml; # 压缩文件类型

Nginx客户端缓存

location ~* .(png|gif|jpg|jpeg)${

expires 1h;

}

Nginx参考文章

Original: https://www.cnblogs.com/wendj/p/15403357.html
Author: begrateful
Title: CentOS7 源码安装Nginx及Nginx基本管理设置

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

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

(0)

大家都在看

  • cocos2d-x for android:ubuntu 环境搭建

    自从体验了vim 的强大之后,好像一开发程序都不自觉的离不开这货了,也倾向在ubuntu 下开发应用。周末闲来无事,看了一下cocos2d,盘算着我也进军游戏界得了。于是乎,装之。…

    Linux 2022年8月26日
    0108
  • 计算机辅助数据绘图(matlabpythonjs)

    1. matlab绘图 官方说明:https://ww2.mathworks.cn/help/matlab/creating_plots/types-of-matlab-plots…

    Linux 2022年11月7日
    079
  • Xshell中文乱码问题

    先查看当前使用的语言: echo $LANG 查看系统的语言安装包: locale 如果没有中文安装包(包含zh_CN字样),需要网络或者自己上传安装包,安装 有了中文语言安装包后…

    Linux 2022年9月14日
    0124
  • docker容器中 -bash: yum: command not found 解决

    作者:沐雪这些文章都是作者原创或翻译的。如有错误,欢迎批评改正。这篇文章的版权属于作者和博客园。请注明是否需要重印。 [En] The articles are all origi…

    Linux 2022年8月30日
    0174
  • windows安装TortoiseGit详细使用教程【基础篇】

    提交代码步骤: 第一步先提交到本地仓库:”Git提交到 -> master”第二步提交到远程仓库:”Git推送” 也就是pus…

    Linux 2022年8月30日
    0113
  • 【Jmeter】jmeter提取response中的返回值,并保存到本地文件–BeanShell后置处理器

    有个需求,需要在压测环境中,创建几十万的账号数据,然后再根据创建结果,查询到某些账号信息。 [En] There is a need to create hundreds of t…

    Linux 2022年9月14日
    0123
  • linux子系统折腾记 (二)

    用wslconfig /u Debian删了系统,输入debian重新安装。过程倒是很简单快捷,就是想不到这个子系统那么容易完蛋。 既然如此就整理一下安装的步骤吧: 选debian…

    Linux 2022年8月20日
    0166
  • 解决USB在虚拟机不显示问题

    电脑重装了下系统,顺带重新装了vmware,发现虚拟机无法识别USB设备,居然连右下角图标和可识别设备都不显示了。 网上找了很多方法,大多是纷繁复杂,且行不行都无法保证。 我发现一…

    Linux 2022年9月10日
    0150
  • 存储更弹性,详解 Fluid “ECI 环境数据访问” 新功能

    近期,Fluid 支持了阿里云 ECI 应用,并将 JuiceFS Runtime Controller 设置为默认安装;JuiceFS 也就此功能与 Fluid 完成了集成和测试…

    Linux 2022年11月8日
    084
  • linux查看ssl证书到期时间

    在linux搭建的服务器上比如CentOS,使用xshell或者putty工具登录后,进入证书目录,可以使用openssl命令查看ssl证书到期时间: # 进入证书目录 cd /u…

    Linux 2022年8月11日
    0252
  • Java基础 String

    String类 字符串是一个特殊的对象。 字符串一旦初始化就不可以被改变。 String s="abc"; 特点: String构造函数 主要几个String构…

    Linux 2022年11月8日
    0100
  • Linux服务器配置git服务

    现在 Github 已经支持个人建立私有仓库,包括国内的一些开源平台如 Gitee 等也支持私有仓库,所以直接去建立私有仓库即可。或者可以自己买服务器搭建 GitLab。但是这篇文…

    Linux 2022年11月7日
    087
  • Git 相关命令

    Git 版本控制系统是一个分布式的系统(与SVN不一样),可用于保存工程源代码及其历史状态的命令行工具。 Git下载地址:https://git-scm.com/downloads…

    Linux 2022年8月30日
    0188
  • MIT6.828——Lab3 PartA(麻省理工操作系统实验)

    Lab3 Part A MIT6.828——Lab1 PartA MIT6.828——Lab1 PartB Lab2内存管理准备知识 MIT6.828——Lab2 内核维护了三个关…

    Linux 2022年9月10日
    0120
  • Linux-进程描述符 task_struct 详解

    为了描述控制进程的运行,系统中存放进程的管理和控制信息的数据结构称为进程控制块 PCB(Process Control Block),它是进程实体的一部分,是操作系统中最重要的记录…

    Linux 2022年8月24日
    0123
  • Linux中修改用户UID和组GID的方法

    我在部署nfs的时候,共享了一个文件夹。为了让远程nfs客户端挂载这个文件夹的时候都有可读写权限,我需要把服务器上的用户uid、gid设置成nfs服务端文件夹一样的权限。不过因为之…

    Linux 2022年8月24日
    0281
最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总