nginx安装

Nginx是一款高性能的HTTP和反向代理服务器,以其轻量级、高并发和稳定性著称。相比Apache,Nginx采用异步逻辑,提供更高效的并发服务。本文详细介绍了Nginx的特性、优点,与Apache的区别,以及Nginx的安装、配置、HTTPS配置、URL重写、反向代理和负载均衡等内容,旨在帮助读者理解和优化Nginx服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ngninx简介

  • Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。
  • Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx的特点与优点

Nginx的特性

Nginx是一个高性能Web和反向代理服务器,它具有很多非常优越的特性:
  • 在高连接并发的情况下,Nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应
  • 使用epoll and kqueue作为开发模型
  • Nginx作为负载均衡服务器:Nginx既可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
  • Nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好很多

Nginx的优点

  • 高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2-3万并发连接数
  • 内存消耗少:在3万并发连接下,开启的10个nginx进程才消耗150M内存(15M*10=150M)
  • 配置文件非常简单:风格跟程序一样通俗易懂
  • 成本低廉:Nginx为开源软件,可以免费使用。而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则钱购买
  • 支持Rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组
  • 内置的健康检查功能:如果Nginx Proxy后端的某台Web服务器宕机了,不会影响前端访问
  • 节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头
  • 稳定性高:用于反向代理,宕机的概率微乎其微
  • 模块化设计:模块可以动态编译
  • 外围支持好:文档全,二次开发和模块较多
  • 支持热部署:可以不停机重载配置文件
  • 支持事件驱动、AIO(AsyncIO,异步IO)、mmap(Memory Map,内存映射)等性能优化

在这里插入图片描述

Nginx和Apache的区别: Nginx 是一款面向性能设计的 HTTP 服务器,相较于 Apache具有占有内存少,稳定性高等优势。与旧版本(<=2.2)的 Apache 不同,Nginx不采用每客户机一线程的设计模型,而是充分使用异步逻辑,削减了上下文调度开销,所以并发服务能力更强。整体采用模块化设计,有丰富的模块库和第三方模块库,配置灵活。在 Linux 操作系统下,Nginx 使用 epoll 事件模型,得益于此,Nginx 在 Linux 操作系统下效率相当高。同时 Nginx 在 OpenBSD 或 FreeBSD 操作系统上采用类似于 epoll 的高效事件模型 kqueue。

反向代理:
代理服务器。
隐藏了真实的服务器,为服务器收发请求,使真实服务器对客户端不可见。
负载均衡服务器,将用户的请求分发到空闲的服务器上。
意味着用户和负载均衡服务器直接通信,即用户解析服务器域名时得到的是负载均衡服务器的 IP。在这里插入图片描述

Nginx 不仅可以做反向代理,实现负载均衡。还能用作正向代理来进行上网等功能。正向代理:如果把局域网外的 Internet 想象成一个巨大的资源库,则局域网中的客户端要访 问 Internet,则需要通过代理服务器来访问,这种代理服务就称为正向代理。

简单一点:通过代理服务器来访问服务器的过程 就叫 正向代理。

需要在客户端配置代理服务器进行指定网站访问
在这里插入图片描述

透明代理的意思是客户端根本不需要知道有代理服务器的存在,它改编你的request fields(报文),并会传送真实IP。注意,加密的透明代理则是属于匿名代理,意思是不用设置使用代理了。
在这里插入图片描述

nginx安装

//关闭防火墙和seliunx
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# vi /etc/selinux/config
//创建系统用户nginx
[oot@localhost ~]# useradd -r -M -s /sbin/nologin ngin
//安装依赖包
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
//安装依赖环境
[root@localhost ~]# yum -y groups mark install 'Development Tools'

//创建日志存放目录
//[root@localhost ~]# mkdir   /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx/
//下载nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.g
//解压
[root@localhost ~]# tar xf nginx-1.18.0.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.18.0  nginx-1.18.0.tar.gz

//编译安装

[root@localhost nginx-1.18.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.18.0]#  make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

nginx安装后配置

//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' >/etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh 
[root@localhost ~]# which nginx
/usr/local/nginx/sbin/nginx


//服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}
[root@localhost ~]# nginx 
[root@localhost ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port                                                    
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*                                                       
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*                                                       
LISTEN 0      128              [::]:22             [::]:*      

在这里插入图片描述

用于调试、定位问题的配置参数

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
daemon off;   //守护进程默认关闭

error_log里的位置和级别能有以下可选项:

位置级别
file stderrsyslog:server=address[,parameter=value] memory:sizedebug:若要使用debug级别,需要在编译nginx时使用–with-debug选项info notice warn error critalert emerg
daemon {on|off};    //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off};    //是否以master/worker模型来运nginx,调试时可以设置为off
[root@localhost conf]# vim/usr/local/nginx/conf/nginx.conf
master_process off;  //关闭master/worker

[root@localhost ~]# ps -ef|grep nginx
root        1746       1  0 15:30 ?        00:00:00 nginx
root        1748    1265  0 15:30 pts/1    00:00:00 grep --color=auto nginx
[root@localhost ~]# ps -ef|grep nginx
root        1751       1  0 15:31 ?        00:00:00 nginx
root        1753    1265  0 15:31 pts/1    00:00:00 grep --color=auto nginx
[root@localhost conf]# vim nginx.conf  error_log  logs/error.log;   //error_log 位置 级别;    //配置错误日志

[root@localhost conf]# tail -f ../logs/error.log  
2020/12/16 15:39:16 [error] 1780#0: *2 open() "/usr/local/nginx/html/123" failed (2: No such file or directory), client: 192.168.236.1, server: localhost, request: "GET /123 HTTP/1.1", host: "192.168.236.135"

正常运行必备的配置参数

[root@localhost conf]# vim nginx.conf
user  nobody;  //指定运行worker进程的用户和组  默认nobody

[root@localhost ~]# ps -ef| grep nginx
root        1803       1  0 15:51 ?        00:00:00 nginx: master process nginx
nobody      1804    1803  0 15:51 ?        00:00:00 nginx: worker process
root        1806    1265  0 15:52 pts/1    00:00:00 grep --color=auto nginx
pid /path/to/pid_file;    //指定nginx守护进程的pid文件
worker_rlimit_nofile number;    //设置所有worker进程最大可以打开的文件数,默认为1024

优化性能的配置参数

[root@localhost conf]# vim nginx.conf
worker_processes  1;   //避免安全上下文的切换,默认为1核心

worker_processes n;    //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...;    //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用3位二进制表示cpu核心,如:
0000 0001 表示1颗核心
0000 0010 表示2颗核心
0000 0100 表示3颗核心
[root@localhost conf]# vim nginx.conf
worker_cpu_affinity 0001;

timer_resolution interval;    //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数

worker_priority number;    //指明worker进程的nice值
[root@localhost conf]# vim nginx.conf
worker_priority -20;
[root@localhost conf]# nginx -s reload //重新加载配置文件

[root@localhost conf]# ps -elf|grep nginx
1 S root        1247       1  0  80   0 - 20089 -      18:12 ?        00:00:00 nginx: master process nginx
5 S nobody      1287    1247  0  60 -20 - 27963 do_epo 18:39 ?        00:00:00 nginx: worker process
0 R root        1292    1222  0  80   0 -  3028 -      18:40 pts/0    00:00:00 grep --color=auto nginx


事件相关的配置:event{}段中的配置参数
accept_mutex {off|on};    //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
[root@localhost conf]# vim nginx.conf

lock_file logs/nginx.lock;
events {
    worker_connections  1024;
    accept_mutex on;  //默认off关闭
}

lock_file file;    //accept_mutex用到的互斥锁锁文件路径
use [epoll | rtsig | select | poll];    //指明使用的事件模型,建议让nginx自行选择
worker_connections #;    //每个进程能够接受的最大连接数
网络连接相关的配置参数
keepalive_timeout number;    //长连接的超时时长,默认为65s
keepalive_requests number;    //在一个长连接上所能够允许请求的最大资源数 默认100 最多200
[root@localhost conf]# vim nginx.conf
  keepalive_requests 110;

keepalive_disable [msie6|safari|none];    //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off;    //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number;    //读取http请求报文首部的超时时长
client_body_timeout number;    //读取http请求报文body部分的超时时长
send_timeout number;    //发送响应报文的超时时长

fastcgi的相关配置参数

LNMP:php要启用fpm模型
配置示例如下

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   $Document_Root$fastcgi_script_name;
           include        fastcgi_params;
        }


nginx作为web服务器时使用的配置:http{}段的配置参数

http{…}:配置http相关,由ngx_http_core_module模块引入。nginx的HTTP配置主要包括四个区块,结构如下:

http { //协议级别
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    #gzip  on;
    upstream {//负载均衡配置
    ...
  }
  server {//服务器级别,每个server类似于httpd中的一个<VirtualHost>
    listen 80;
    server_name localhost;
    location / {//请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系
      root html;
      index index.html index.htm;
    }
  }

http{}段配置指令:

server {}:定义一个虚拟主机:

 server {
        listen       80;
        server_name  localhost;
         root "/vhosts/web";
        }

listen:指定监听的地址和端口

listen address[:port];
listen port;

server_name NAME […]; 后面可跟多个主机,名称可使用正则表达式或通配符

当有多个server时,匹配顺序如下:

1.先做精确匹配检查
2.左侧通配符匹配检查,如*.idfsoft.com
3.右侧通配符匹配检查,如mail.*
4.正则表达式匹配检查,如~ ^.*.idfsoft.com$
5.default_server

root path; 设置资源路径映射,用于指明请求的URL所对应的资源所在的文件系统上的起始路径
alias path; 用于location配置段,定义路径别名

index file; 默认主页面

location / {
            root   html;
            index  index.html index.htm;
        }

error_page code […] [=code] URI | @name 根据http响应状态码来指明特用的错误页面,例如 error_page 404 /404_customed.html

[=code]:以指定的响应码进行响应,而不是默认的原来的响应,默认表示以新资源的响应码为其响应码,例如 error_page 404 =200 /404_customed.html

log_format 定义日志格式

//取消注释
   # 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;

[root@localhost ~]# cd  /usr/local/nginx/logs/

[root@localhost logs]# ls
access.log  error.log  nginx.pid

[root@localhost logs]# tail -f access.log 
192.168.236.1 - - [20/Dec/2020:21:00:57 +0800] "GET / HTTP/1.1" 200 84066 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66" "-"
192.168.236.1 - - [20/Dec/2020:21:01:11 +0800] "GET /fcje HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66" "-"

统计某个时间段,Nginx日志访问最频繁的IP
[root@localhost logs]# awk '/20\/Dec\/2020/ {ips[$1]++}  END {for(i in ips){print ips[i],i}}' access.log | sort -rn | head
2 192.168.236.1
[root@localhost logs]# 

常用修饰符说明:

修饰符功能
=精确匹配
~正则表达式模式匹配,区分大小写
~*正则表达式模式匹配,不区分大小写
^~前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等

查找顺序和优先级:由高到底依次为

1.带有=的精确匹配优先
2.正则表达式按照他们在配置文件中定义的顺序
3.带有^~修饰符的,开头匹配
4.带有*修饰符的,如果正则表达式与URI匹配
5.没有修饰符的精确匹配

访问控制

用于location段
allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny:设定禁止哪台或哪些主机访问,多个参数间用空格隔开

allow 192.168.236.135/24 172.16.0.0/8;
deny all;

基于用户认证
auth_basic "你好";
auth_basic_user_file "/path/to/user_auth_file"

user_auth_file内容格式为:

username:password

这里的密码为加密后的密码串,建议用htpasswd来创建此文件:

htpasswd -c -m /path/to/.user_auth_file USERNAME

NGINX平滑升级

//启动nginx
[root@localhost ~]# nginx

1.查看之前编译的参数

//查看之前编译的参数
[root@localhost ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log

2.下载新模块

//下载echo模块
[root@localhost ~]# wget https://github.com/openresty/echo
 nginx-module.git
[root@localhost ~]# yum -y install unzip
//解压
[root@localhost ~]# unzip echo-nginx-module-master.zip 

//解压nginx
[root@localhost ~]# tar xf nginx-1.18.0.tar.gz
[root@localhost ~]# cd nginx-1.18.0

3.编译新模块

//编译新模块
在原有的参数加上之前的编译和--add-module=新模块路径 
[root@localhost nginx-1.18.0]# --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master

//编译
[root@localhost nginx-1.18.0]# make

[root@localhost objs]# ./nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master

3.备份原程序

[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx 
[root@localhost sbin]# cp nginx nginx-bake
[root@localhost sbin]# ls
nginx  nginx-bake

4.拷贝新程序

[root@localhost ~]# cd nginx-1.18.0/objs/
[root@localhost objs]# 
[root@localhost objs]# pwd
/root/nginx-1.18.0/objs
//启动nginx
[root@localhost objs]# /usr/local/nginx/sbin/nginx -s stop;\cp nginx /usr/local/nginx/sbin/;/usr/local/nginx/sbin/nginx

[root@localhost ~]# which nginx
/usr/local/nginx/sbin/nginx

https配置

生成私钥,生成签证请求并获得证书,然后在nginx.com配置

//CA生成一对密钥
[root@localhost ~]# mkdir /etc/pki/CA
[root@localhost ~]# cd /etc/pki/CA/
[root@localhost CA]# mkdir private
[root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048) 
Generating RSA private key, 2048 bit long modulus (2 primes)
........................................................+++++
..................................................................+++++
e is 65537 (0x010001)
//CA生成自签署证书
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365 
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:hb
Locality Name (eg, city) [Default City]:wh
Organization Name (eg, company) [Default Company Ltd]:runtime
Organizational Unit Name (eg, section) []:peixu
Common Name (eg, your name or your server's hostname) []:www.example.com
Email Address []:1@2.com

[root@localhost CA]# mkdir certs newcerts crl
[root@localhost CA]# touch index.txt && echo 01 > serial
[root@localhost CA]# ls
cacert.pem  crl        newcerts  serial
certs       index.txt  private

[root@localhost ~]# mkdir ssl
[root@localhost ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
.................+++++
....................................................................+++++
e is 65537 (0x010001)
//客户端生成证书签署请求
[root@localhost ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
Ignoring -days; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:hb
Locality Name (eg, city) [Default City]:wh
Organization Name (eg, company) [Default Company Ltd]:runtime
Organizational Unit Name (eg, section) []:www.example.com
Common Name (eg, your name or your server's hostname) []:1@2.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
//CA签署客户端提交上来的证书
[root@localhost ssl]# openssl ca -in ./nginx.csr  -out nginx.crt -days 365


[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

    server {
        listen       443 ssl;
        server_name  www.example.com;

        ssl_certificate      ../ssl/nginx.crt;
        ssl_certificate_key  ../ssl/nginx.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

}

在这里插入图片描述

rewrite从写url

语法:rewrite regex replacement flag;,如:

rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# mkdir img
[root@nginx html]# ls
  index.html
[root@nginx html]# cd img/
[root@localhost img]# ls
1.jpg

在这里插入图片描述

常见的flag

flag作用
last基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程
break中止Rewrite,不再继续匹配一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求,且不再会被当前location内的任何rewrite规则所检查
redirect以临时重定向的HTTP状态302返回新的URL
permanent以永久重定向的HTTP状态301返回新的URL

rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)

nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:

标识符意义
^必须以^后的实体开头
$必须以$前的实体结尾
.匹配任意字符
[]匹配指定字符集内的任意字符
[^]匹配任何不包括在指定字符集内的任意字符串
|匹配 之前或之后的实体
()分组,组成一组用于匹配的实体,通常会有

if

语法:if (condition) {…}

应用场景:

server段
location段
常见的condition

  • 变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
  • 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
    正则表达式的模式匹配操作
  • ~:区分大小写的模式匹配检查
  • ~*:不区分大小写的模式匹配检查
  • !和!*:对上面两种测试取反
  • 测试指定路径为文件的可能性(-f,!-f)
  • 测试指定路径为目录的可能性(-d,!-d)
  • 测试文件的存在性(-e,!-e)
  • 检查文件是否有执行权限(-x,!-x
反向代理与负载均衡

三台服务器

在RS1和RS2上安装服务

[root@rs1 ~]# yum -y install httpd
[root@rs1 ~]# systemctl start httpd
[root@rs1 ~]#  ss -antl
State  Recv-Q  Send-Q   Local Address:Port           Peer Address:Port      
LISTEN 0       128            0.0.0.0:22                  0.0.0.0:*         
LISTEN 0       128               [::]:22                     [::]:*         
LISTEN 0       128                  *:80                        *:*    
[root@rs1 ~]#
[root@rs1 ~]# cd /var/www/html/
[root@rs1 html]# echo 'rs1' > index.html

在这里插入图片描述

[root@rs2 ~]# yum -y install httpd
[root@rs2 ~]# systemctl start httpd
[root@rs2 ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port                                                    
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*                                                       
LISTEN 0      100         127.0.0.1:25          0.0.0.0:*                                                       
LISTEN 0      128                 *:80                *:*                                                       
LISTEN 0      128              [::]:22             [::]:*                                                       
LISTEN 0      100             [::1]:25             [::]:*         
[root@rs2 html]# cd /var/www/html/
                                           
[root@rs2 html]# echo 'rs2' > index.html

在这里插入图片描述

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

    upstream www.runtime.com {
       server 192.168.236.141;
       server 192.168.236.131;
    }
         server {
        listen       80;
        server_name  www.example.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                                         这里做修改
            proxy_pass http://www.runtime.com;
        }

       #error_page  404              /404.html;
......
    [root@localhost ~]# nginx -s reload
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值