0009 -- Nginx

文章目录

部署服务

raid 分区

# 新增4块10G的硬盘,进行raid10

[root@edon ~]# mkfs.xfs /dev/md0

[root@edon ~]# mkdir /Edon_raid

[root@edon ~]# mount /dev/md0 /Edon_raid/

[root@edon ~]# mount -l | grep /dev/md0
/dev/md0 on /Edon_raid type xfs (rw,relatime,attr2,inode64,sunit=1024,swidth=2048,noquota)

[root@edon ~]# df -hT | grep /dev/md0
/dev/md0                xfs        20G   33M   20G    1% /Edon_raid

[root@edon ~]# mdadm -D /dev/md0

关闭防火墙与selinux机制

1. 关闭防火墙、并禁止开机自启动
[root@s ~]# systemctl stop firewalld
[root@s ~]# systemctl disable firewalld

2. 临时关闭selinux,并修改配置文件永久关闭
[root@s ~]# setenforce 0
setenforce: SELinux is disabled
[root@s ~]# vim /etc/selinux/config
[root@s ~]# grep -i '^selinu' /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted

3. 清空防火墙安全机制
[root@s ~]# iptables -F
[root@s ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

阿里镜像源

yum -y install wget 
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache

HTTP 服务

httpd:阿帕奇

# yum部署服务
[root@S ~]# yum -y install httpd

# 添加httpd开机自启
[root@S ~]# systemctl start httpd
[root@S ~]# systemctl enable httpd

# 检查程序进程和端口
[root@S ~]# netstat -tunlp | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      1592/httpd
[root@S ~]# ps -ef | grep httpd
root       1592      1  0 21:18 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     1594   1592  0 21:19 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     1595   1592  0 21:19 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     1596   1592  0 21:19 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     1597   1592  0 21:19 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     1598   1592  0 21:19 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

# 测试页面
[root@S ~]# vim /var/www/html/index.html
[root@S ~]# curl 127.0.0.1
<meta charset=utf-8>
Edon 学习Apache部署搭建!!!

# 更改默认网页index.html
[root@S ~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/www"

<Directory "/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# 创建资源目录,创建html文件
[root@S ~]# mkdir -m 755 /www
[root@S ~]# cd /www/
[root@S www]# vim index.html
<meta charset=utf8>
我是新的首页,很开心能实现默认目录的转移!!!

# 修改了配置文件,还得重启http服务才能生效
[root@S www]# systemctl restart httpd
[root@S www]# curl 192.168.75.6
<meta charset=utf8>
我是新的首页,很开心能实现默认目录的转移!!!

# 修改监听端口
[root@S ~]# vim /etc/httpd/conf/httpd.conf
[root@S ~]# grep -i '^listen' /etc/httpd/conf/httpd.conf
Listen 86
ServerName localhost:86
[root@S ~]# systemctl restart httpd
[root@S ~]# curl 127.0.0.1:86
<meta charset=utf8>
我是新的首页,很开心能实现默认目录的转移!!!

apache配置文件

## apache配置文件
Linux下学习软件使用,以及针对软件定制各种功能,其实就是在修改配置文件,因此我们得知道各配置文件的位置

| 文件路径                               | 作用                         |
| ------------------------------------  | ----------------------------|
| /etc/httpd/conf/httpd.conf            | apache主配置文件             |
| /etc/httpd                            | apache主配置目录             |
| /etc/httpd/conf.d/*.conf              | apache子配置文件             |
| /usr/sbin/httpd                       | 二进制脚本                   |
| /var/log/httpd/                       | 日志路径access_log error_log |
| /var/www/html                         | 站点资源目录                 |
| /usr/lib/systemd/system/httpd.service | httpd服务脚本文件            |
| /usr/lib64/httpd/modules/             | httpd模块文件路径            |
[root@S ~]# curl -o /dev/null -s -w %{http_code} www.baidu.com
200[root@S ~]#
[root@S ~]# curl -o /dev/null -s -w %{http_code} 192.168.75.6:86
200[root@S ~]#
[root@S ~]# curl -o /dev/null -s -w %{http_code} 192.168.75.6
000[root@S ~]#

Nginx 服务

01. Nginx架构

image-20200210162748550

02. Nginx安装包

Nginx分为商业版,开源版,支持Linux与Windows平台下载使用

nginx.com  商业版
nginx.org  开源版
https://tengine.taobao.org/  淘宝二次开发版

03. 安装前环境准备

0. 永久性修改主机名
[root@s ~]# hostnamectl set-hostname s

1. 查看操作系统
[root@s ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)

2. 安装GCC编译环境
[root@s ~]# yum install -y gcc gcc-c++ autoconf automake make

3. 解决模块依赖性:Nginx支持的功能模块需要有第三方的库支持
   例如gzip的zlib库、rewrite重写需要的pcre库、HTTPS需要的openssl库等等...
[root@s ~]# yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel wgett httpd-tools vim

4. 安装系统基础开发环境
[root@s ~]# yum groupinstall -y  "Development Tools" "Basic Web Server"

5. 关闭防火墙、关闭selinux机制
[root@s ~]# systemctl stop firewalld
[root@s ~]# systemctl disable firewalld
[root@s ~]# setenforce 0
setenforce: SELinux is disabled
[root@s ~]# vim /etc/selinux/config
[root@s ~]# grep -i '^selinu' /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
[root@s ~]# iptables -F
[root@s ~]# iptables -L

6. 检查yum源配置正确
[root@s ~]# ls -l /etc/yum.repos.d/
总用量 12
drwxr-xr-x. 2 root root  220 32 2021 bak
-rw-r--r--  1 root root 2523 1226 2020 CentOS-Base.repo
-rw-r--r--  1 root root  664 1226 2020 epel.repo
-rw-r--r--. 1 root root   69 32 2021 local.repo

7. 网络连接状态正常
[root@s ~]# ping -c 2 baidu.com

04. Nginx 源码安装及介绍

Nginx源代码,下载地址:https://nginx.org/en/download.html

  • yum自动安装,不支持自由扩展第三方功能
  • 源码编译安装
Mainline version 主流版本
Stable version   稳定版本
Legacy versions  旧版本
1. 创建nginx的目录,并设置755的权限
[root@s ~]# mkdir -m 755 Learn_Nginx

2. 官网下载Nginx源代码 主流版本 1.23.1、稳定版本 1.22.0
[root@s Learn_Nginx]# wget https://nginx.org/download/nginx-1.23.1.tar.gz
[root@s Learn_Nginx]# wget https://nginx.org/download/nginx-1.22.0.tar.gz

3. 解压缩Nginx源代码
[root@s Learn_Nginx]# tar -zxf nginx-1.22.0.tar.gz
[root@s Learn_Nginx]# tar -zxf nginx-1.23.1.tar.gz
[root@s Learn_Nginx]# ll
总用量 2132
drwxr-xr-x 8 1001 1001     158 524 07:59 nginx-1.22.0
-rw-r--r-- 1 root root 1073322 524 22:29 nginx-1.22.0.tar.gz
drwxr-xr-x 8 1001 1001     158 719 22:05 nginx-1.23.1
-rw-r--r-- 1 root root 1104352 719 22:30 nginx-1.23.1.tar.gz

4. 复制Nginx默认提供的vim语法插件
[root@s nginx-1.22.0]# pwd
/root/Learn_Nginx/nginx-1.22.0
[root@s nginx-1.22.0]# mkdir ~/.vim
[root@s nginx-1.22.0]# cp -r contrib/vim/* ~/.vim

5. Nginx源代码目录介绍
[root@s nginx-1.22.0]# ll
总用量 800
drwxr-xr-x 6 1001 1001    326 88 15:04 auto        # 检测系统模块
-rw-r--r-- 1 1001 1001 317070 524 07:59 CHANGES     # nginx更改记录文件
-rw-r--r-- 1 1001 1001 484445 524 07:59 CHANGES.ru
drwxr-xr-x 2 1001 1001    168 88 15:07 conf        # 存放nginx配置文件
-rwxr-xr-x 1 1001 1001   2590 524 07:59 configure   # 释放编译文件的定制脚本
drwxr-xr-x 4 1001 1001     72 88 15:04 contrib     # 提供了perl与vim插件
drwxr-xr-x 2 1001 1001     40 88 15:04 html        # 存放标准html页面语法
-rw-r--r-- 1 1001 1001   1397 524 07:59 LICENSE
drwxr-xr-x 2 1001 1001     21 88 15:04 man
-rw-r--r-- 1 1001 1001     49 524 07:59 README
drwxr-xr-x 9 1001 1001     91 88 15:04 src         # 存放nginx源码

6. 开始编译Nginx,扩展编译模块
   列出Nginx的编译选项:如制定安装路径,配置文件、日志文件等路径,指定开启模块功能等等...
[root@s nginx-1.22.0]# ./configure --help

7. (编译三部曲):第一步  
  编译 Nginx  
[root@chaogelinux nginx-1.14.0]# ./configure --prefix=/home/Learn_Nginx/nginx/ --with-http_ssl_module  --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module  --with-threads  --with-file-aio

8. (编译三部曲):第二步  
  执行make编译 
[root@s nginx-1.22.0]# make

9. (编译三部曲):第三步 
  首次编译安装,生成Nginx的可执行命令 
[root@s nginx-1.22.0]# make install

10. 检查Prefix指定的安装目录
[root@s ~]# ls -l /home/Learn_Nginx/
总用量 0
drwxr-xr-x 6 root root 54 88 15:24 nginx

11. Nginx的程序目录
[root@s nginx]# pwd
/home/Learn_Nginx/nginx
[root@s nginx]# ll
总用量 0
drwxr-xr-x 2 root root 333 88 15:24 conf  # 配置文件
drwxr-xr-x 2 root root  40 88 15:24 html  # 静态文件
drwxr-xr-x 2 root root   6 88 15:24 logs  # 日志
drwxr-xr-x 2 root root  36 88 15:31 sbin  # 二进制命令

12.创建 nginx 的环境变量文件,修改如下,创建/etc/profile.d/nginx.sh脚本文件便于以后维护
[root@s ~]# echo "export PATH=/home/Learn_Nginx/nginx/sbin:$PATH" >> /etc/profile.d/nginx.sh
[root@s ~]# cat /etc/profile.d/nginx.sh
export PATH=/home/Learn_Nginx/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

13. source 配置文件 或者 退出会话,重新登录终端,此时可以正常使用nginx
[root@s ~]# source /etc/profile.d/nginx.sh
[root@s ~]# echo $PATH
/home/Learn_Nginx/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

14. 检查nginx的编译模块信息
[root@s ~]# nginx -V
nginx version: nginx/1.22.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=/home/Learn_Nginx/nginx/ --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio

15. 启动Nginx服务,检查进程 (这里一定要用绝对路径启动,为之后的热部署做准备)
[root@s ~]# /home/Learn_Nginx/nginx/sbin/nginx
[root@s ~]# ps -ef | grep nginx | grep -v grep
root      59981      1  0 17:10 ?        00:00:00 nginx: master process /home/Learn_Nginx/nginx/sbin/nginx
nobody    59982  59981  0 17:10 ?        00:00:00 nginx: worker process

浏览器测试:http://192.168.75.6/

image-20220807171553353

05. Nginx 配置文件语法

  • nginx.conf 由指令与指令块构成
  • 每行语句由分号结束,指令和参数之间由空格分隔
  • 指令块可以大括号{}组织多条指令块
  • 配置文件中 #号 添加注释信息
  • 支持 $变量 使用变量
  • 支持include语句,组合多个配置文件
  • 部分指令支持正则表达式

image-20200211094210904

06. nginx.conf 配置文件详解

######Nginx配置文件nginx.conf中文详解#####

# 定义Nginx运行的 用户和 用户组;
user www www;


# nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;


# 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /usr/local/nginx/logs/error.log info;


# Nginx进程pid号文件
pid /usr/local/nginx/logs/nginx.pid;


# 指定进程可以打开的最大描述符:数目
# 工作模式与连接数上限
# 这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分 配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。
# 现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。
# 这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。
worker_rlimit_nofile 65535;


events
{
   
    #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
    #是Linux 2.6以上版本内核中的高性能网络I/O模型,linux建议epoll,如果跑在FreeBSD上面,就用kqueue模型。
    #补充说明:
    #与apache相类,nginx针对不同的操作系统,有不同的事件模型
    #A)标准事件模型
    #Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
    #B)高效事件模型
    #Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
    #Epoll:使用于Linux内核2.6版本及以后的系统。
    #/dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
    #Eventport:使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。
    use epoll;


    #单个进程最大连接数(最大连接数=连接数*进程数)
    #根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。
    worker_connections 65535;

    #keepalive超时时间。
    keepalive_timeout 60;

    #客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
    #分页大小可以用命令getconf PAGESIZE 取得。
    # [root@s ~]# getconf PAGESIZE
    #  4096
    #但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
    client_header_buffer_size 4k;

    #这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
    open_file_cache max=65535 inactive=60s;

    #这个是指多长时间检查一次缓存的有效信息。
    #语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.
    open_file_cache_valid 80s;

    #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
    #语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location  这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.
    open_file_cache_min_uses 1;

    #语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.
    open_file_cache_errors on;
}



#设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
   
    #文件扩展名与文件类型映射表
    include mime.types;

    #默认文件类型
    default_type application/octet-stream;

    #默认编码
    #charset utf-8;

    #服务器名字的hash表大小
    #保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.
    server_names_hash_bucket_size 128;

    #客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
    client_header_buffer_size 32k;

    #客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果header过大,它会使用large_client_header_buffers来读取。
    large_client_header_buffers 4 64k;

    #设定通过nginx上传文件的大小
    client_max_body_size 8m;

    #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    sendfile on;

    #开启目录列表访问,合适下载服务器,默认关闭。
    autoindex on;

    #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
    tcp_nopush on;

    tcp_nodelay on;

    #长连接超时时间,单位是秒
    keepalive_timeout 120;

    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    #gzip模块设置
    gzip on;               #开启gzip压缩输出
    gzip_min_length 1k;    #最小压缩文件大小
    gzip_buffers 4 16k;    #压缩缓冲区
    gzip_http_version 1.0;    #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 2;     #压缩等级
    gzip_types text/plain application/x-javascript text/css application/xml;    #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;

    #开启限制IP连接数的时候需要使用
    #limit_zone crawler $binary_remote_addr 10m;



    #负载均衡配置
    upstream pythonav.cn {
   

        #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
        server 192.168.80.121:80 weight=3;
        server 192.168.80.122:80 weight=2;
        server 192.168.80.123:80 weight=3;

        #nginx的upstream目前支持4种方式的分配
        #1、轮询(默认)
        #每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
        #2、weight
        #指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
        #例如:
        #upstream bakend {
   
        #    server 192.168.0.14 weight=10;
        #    server 192.168.0.15 weight=10;
        #}
        #2、ip_hash
        #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
        #例如:
        #upstream bakend {
   
        #    ip_hash;
        #    server 192.168.0.14:88;
        #    server 192.168.0.15:80;
        #}
        #3、fair(第三方)
        #按后端服务器的响应时间来分配请求,响应时间短的优先分配。
        #upstream backend {
   
        #    server server1;
        #    server server2;
        #    fair;
        #}
        #4、url_hash(第三方)
        #按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
        #例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
        #upstream backend {
   
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值