linux的nginx的安装和使用
发布日期:2022-03-30 05:03:19 浏览次数:49 分类:博客文章

本文共 20780 字,大约阅读时间需要 69 分钟。

安装,启动nginx

1.下载源码包wget -c https://nginx.org/download/nginx-1.12.0.tar.gz 2.解压缩源码 tar -zxvf nginx-1.12.0.tar.gz 3.配置,编译安装  开启nginx状态监测功能 ./configure --prefix=/opt/nginx1-12/ --with-http_ssl_module --with-http_stub_status_module make && make install 4.启动nginx,进入sbin目录,找到nginx启动命令 cd sbin ./nginx #启动 ./nginx -s stop #关闭 ./nginx -s reload #重新加载

安装完成后检测服务

netstat -tunlp |grep 80 curl -I 127.0.0.1 #如果访问不了,检查selinux,iptables

部署一个web站点

nginx默认站点是Nginx目录下的html文件夹,这里可以从nginx.conf中查到

location /{            root   html;  #这里是默认的站点html文件夹,也就是 /opt/nginx1-12/html/文件夹下的内容            index  index.html index.htm; #站点首页文件名是index.html        }

如果要部署网站业务数据,只需要把开发好的程序全放到html目录下即可

[root@oldboy_python /tmp 11:34:52]#ls /opt/nginx1-12/html/index.html  jssts.jpeg  lhy.mp4  man.jpg  wget-log

因此只需要通过域名/资源,即可访问

http://www.pyyuc.cn/man.jpg

Nginx的目录结构

[root@oldboy_python /opt/nginx1-12 11:44:02]#lsclient_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  static  uwsgi_temp
  • conf 存放nginx所有配置文件的目录,主要nginx.conf
  • html 存放nginx默认站点的目录,如index.html、error.html等
  • logs 存放nginx默认日志的目录,如error.log access.log
  • sbin 存放nginx主命令的目录,sbin/nginx

Nginx主配置文件解析

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

CoreModule核心模块user www;                       #Nginx进程所使用的用户worker_processes 1;             #Nginx运行的work进程数量(建议与CPU数量一致或auto)error_log /log/nginx/error.log  #Nginx错误日志存放路径pid /var/run/nginx.pid          #Nginx服务运行后产生的pid进程号
events事件模块events {                worker_connections  //每个worker进程支持的最大连接数    use epool;          //事件驱动模型, epoll默认}
http内核模块//公共的配置定义在http{}http {  //http层开始...        //使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)    'server' {        listen       80;        //监听端口, 默认80        server_name  localhost; //提供服务的域名或主机名        access_log host.access.log  //访问日志        //控制网站访问路径        'location' / {            root   /usr/share/nginx/html;   //存放网站代码路径            index  index.html index.htm;    //服务器返回的默认页面文件        }        //指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton        error_page   500 502 503 504  /50x.html;    }    ...    //第二个虚拟主机配置    'server' {    ...    }        include /etc/nginx/conf.d/*.conf;  //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件}   //http层结束

Nginx虚拟主机

 

如果每台linux服务器只运行了一个小网站,那么人气低,流量小的草根站长需要承担高额的服务器租赁费,也造成了硬件资源浪费。

虚拟主机就是将一台服务器分割成多个“虚拟服务器”,每个站点使用各自的硬盘空间,由于省资源,省钱,众多网站都使用虚拟主机来部署网站。

虚拟主机的概念就是在web服务里的一个独立的网站站点,这个站点对应独立的域名(IP),具有独立的程序和资源目录,可以独立的对外提供服务。 这个独立的站点配置是在nginx.conf中使用server{}代码块标签来表示一个虚拟主机。 Nginx支持多个server{}标签,即支持多个虚拟主机站点。

虚拟主机类型

基于域名的虚拟主机通过不同的域名区分不同的虚拟主机,是企业应用最广的虚拟主机。 基于端口的虚拟主机 通过不同的端口来区分不同的虚拟主机,一般用作企业内部网站,不对外直接提供服务的后台,例如www.pythonav.cn:9000 基于IP的虚拟主机 通过不同的IP区分不同的虚拟主机,此类比较少见,一般业务需要多IP的常见都会在负载均衡中绑定VIP

Nginx状态信息(status)配置

Nginx状态信息(status)配置及信息详解    nginx与php-fpm一样内建了一个状态页,对于想了解nginx的状态以及监控nginx非常有帮助。为了后续的zabbix监控,我们需要先了解一下nginx的状态页。Nginx状态信息(status)介绍    Nginx软件在编译时又一个with-http_stub_status_module模块,这个模块功能是记录Nginx的基本访问状态信息,让使用者了解Nginx的工作状态。 要想使用状态模块,在编译时必须增加--with-http_stub_status_module参数。

监测你的nginx是否安装了status模块

[root@master conf]# /opt/nginx/sbin/nginx -Vnginx version: nginx/1.12.0built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)configure arguments: --prefix=/opt/nginx/ --with-http_stub_status_module

启动status状态功能,修改配置文件

#在访问ip/status的时候,进入状态功能        location /status {         #开启nginx状态功能             stub_status on;}

平滑重启nginx

./sbin/nginx -s reload

访问status页面

http://192.168.119.10/status

通过ab压测命令检测

-n requests #执行的请求数,即一共发起多少请求。

-c concurrency #请求并发数。

-#启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。

ab -kc 1000 -n 100000 http://192.168.119.10/

 

status页面解析

 

 

 

 

 

基于域名的多虚拟主机实战

nginx可以自动识别用户请求的域名,根据不同的域名请求服务器传输不同的内容,只需要保证服务器上有一个可用的ip地址,配置好dns解析服务。

/etc/hosts是linux系统中本地dns解析的配置文件,同样可以达到域名访问效果

修改nginx.conf

[root@oldboy_python ~ 14:33:16]#egrep -v '#|^$' /opt/nginx1-12/conf/nginx.confworker_processes  1;events {    worker_connections  1024;}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;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  www.pyyuc.cn;        location /{            root   html/pyyuc;            index  index.html index.htm;        }    }}

上述代码配置了一个www.pyyuc.cn域名的站点,虚拟主机的部分就是server{}里的内容

创建pyyuc.cn的站点目录和文件

[root@oldboy_python /opt/nginx1-12/html 14:36:08]#mkdir pyyuc

[root@oldboy_python /opt/nginx1-12/html 14:36:18]#echo "<meta charset=utf8>我是pyyuc站点" > pyyuc/index.html

[root@oldboy_python /opt/nginx1-12/html 14:37:21]#cat pyyuc/index.html
<meta charset=utf8>我是pyyuc站点

上述作用创建了一个html/pyyuc站点目录,对应于虚拟主机配置文件里的root根目录的设置html/pyyuc

然后生成一个首页文件index.html,内容是“我是pyyuc站点”

检查nginx语法重新加载nginx

[root@oldboy_python /opt/nginx1-12/html 14:37:28]#../sbin/nginx -t

nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful

#平滑重启nginx

[root@oldboy_python /opt/nginx1-12/html 14:39:18]#../sbin/nginx -s reload

检查nginx端口,进程,访问pyyuc虚拟站点

[root@oldboy_python /opt/nginx1-12/html 14:40:02]#netstat -tunlp|grep nginx [root@oldboy_python /opt/nginx1-12/html 14:40:29]#ps -ef|grep nginx #我这里是有dns解析,没有的话则需要/etc/hosts解析 #成功配置了pyyuc虚拟主机站点 [root@oldboy_python /opt/nginx1-12/html 14:41:37]#curl www.pyyuc.cn 
我是pyyuc站点

配置多个域名的虚拟主机

其实就是新增一个server{}虚拟主机

egrep -v '#|^$' /opt/nginx1-12/conf/nginx.confworker_processes  1;events {    worker_connections  1024;}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;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  www.pyyuc.cn;        location /{            root   html/pyyuc;            index  index.html index.htm;        }}    server {        listen       80;        server_name  www.pythonav.cn;        location /{            root   html/pythonav;            index  index.html index.htm;        }}    }

创建pythonav虚拟主机站点的目录和文件

[root@oldboy_python /opt/nginx1-12 14:47:21]#mkdir -p /opt/nginx1-12/html/pythonav [root@oldboy_python /opt/nginx1-12 14:49:33]#echo "
我是pythonav,未成年禁止入内"> /opt/nginx1-12/html/pythonav/index.html
[root@oldboy_python /opt/nginx1-12 14:50:44]#./sbin/nginx -tnginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is oknginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful[root@oldboy_python /opt/nginx1-12 14:51:32]#./sbin/nginx -s reload

大功告成,基于域名的虚拟主机实战搞定

[root@oldboy_python /opt/nginx1-12 14:52:12]#curl www.pythonav.cn
我是pythonav,未成年禁止入内[root@oldboy_python /opt/nginx1-12 14:52:40]#curl www.pyyuc.cn
我是pyyuc站点

nginx访问日志(access_log)

日志功能对每个用户访问网站的日志信息记录到指定的日志文件里,开发运维人员可以分析用户的浏览器行为,此功能由ngx_http_log_module模块负责,官网地址是:

http://nginx.org/en/docs/http/ngx_http_log_module.html

控制日志的参数

log_format    记录日志的格式,可定义多种格式accsss_log    指定日志文件的路径以及格式

  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    对应[14/Aug/2018:18:46:52 +0800] $request     对应请求信息"GET /favicon.ico HTTP/1.1" $status      状态码 $body_bytes_sent  571字节 请求体的大小 $http_referer  对应“-”  由于是直接输入浏览器就是 - $http_user_agent  客户端身份信息 $http_x_forwarded_for  记录客户端的来源真实ip 97.64.34.118

日志效果如下

66.102.6.6 - - [14/Aug/2018:18:46:52 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "-"  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 Google Favicon" "97.64.34.118"

 nginx.conf默认配置

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;

日志格式配置定义

log_format是日志关键字参数,不能变main是日志格式指定的标签,记录日志时通过main标签选择指定的格式。 

nginx限制网站来源IP访问

如果哪天发现你的nginx很慢,或者检查access.log时候,有一个some body疯狂请求你的nginx server,那么可以禁止这个IP访问
限制ip或ip段访问 禁止访问/av/底下的资源 location /av {deny 122.71.240.254;#alias /opt/nginx1-12/html/av;allow 10.1.1.0/16;  }

 

Nginx错误页面优化

在网站运行过程中,可能因为页面不存在等原因,导致网站无法正常响应请求,此时web服务会返回系统的错误码,但是默认的错误页面很不友好。

 

因此我们可以将404,403等页面的错误信息重定向到网站首页或者其他指定的页面,提升用户访问体验。

server {        listen       80;        server_name  www.pythonav.cn;        root html/pythonav;        location /{            index  index.html index.htm;        }       #在pythonav路径下的40x.html错误页面        error_page 400 403 404 405 /40x.html;        }

40x.html

此时访问www.pythonav.cn/asdasd错误页面已经优化了

 

Nginx代理

正向代理

正向代理,也就是传说中的代理,他的工作原理就像一个跳板(VPN),简单的说:

我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的网站,于是我先连上代理服务器,告诉他我需要那个无法访问网站的内容,代理服务器去取回来,然后返回给我。

 

反向代理

对于客户端而言,代理服务器就像是原始服务器。

nginx实现负载均衡的组件

ngx_http_proxy_module    proxy代理模块,用于把请求抛给服务器节点或者upstream服务器池

实现一个简单的反向代理

机器准备,两台服务器

master  192.168.11.63  主负载 slave   192.168.11.64  web1

主负载均衡节点的配置文件

worker_processes  1;error_log  logs/error.log;pid        logs/nginx.pid;events {    worker_connections  1024;}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;    sendfile        on;    keepalive_timeout  65;        upstream slave_pools{    server 192.168.11.64:80 weight=1;}    server {        listen       80;        server_name  localhost;        location / {        proxy_pass  http://slave_pools;            root   html;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}
nginx.conf

检查语法并启动nginx

[root@master 192.168.11.63 /opt/nginx1-12]$/opt/nginx1-12/sbin/nginx -tnginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is oknginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
#启动nginx [root@master 192.168.11.63 /opt/nginx1-12]$/opt/nginx1-12/sbin/nginx #检查端口 [root@master 192.168.11.63 /opt/nginx1-12]$netstat -tunlp|grep nginx tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8921/nginx: master

此时访问master的服务器192.168.11.63:80地址,已经会将请求转发给slave的80端口

除了页面效果的展示以外,还可以通过log(access.log)查看代理效果

master端日志

slave端日志

参考:

Nginx负载均衡概述

Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中, 实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾

 

Nginx要实现负载均衡需要用到proxy_pass代理模块配置

Nginx负载均衡与Nginx代理不同地方在于

Nginx代理仅代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池

Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用。

 upstream配置

在nginx.conf > http 区域中

upstream django {       server 10.0.0.10:8000;       server 10.0.0.11:9000;}

在nginx.conf > http 区域 >  server区域  > location配置中

添加proxy_pass

location / {            root   html;            index  index.html index.htm;            proxy_pass http://django;}

此时初步负载均衡已经完成,upstream默认按照轮训方式负载,每个请求按时间顺序逐一分配到后端节点。

upstream分配策略

weight 权重

upstream django {       server 10.0.0.10:8000 weight=5;       server 10.0.0.11:9000 weight=10;#这个节点访问比率是大于8000的}

ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器
upstream django {     ip_hash;       server 10.0.0.10:8000;       server 10.0.0.11:9000;}

backup

在非backup机器繁忙或者宕机时,请求backup机器,因此机器默认压力最小

upstream django {       server 10.0.0.10:8000 weight=5;       server 10.0.0.11:9000;       server node.oldboy.com:8080 backup;}

负载均衡实验环境规划

角色            ip                    主机名lb01        192.168.119.10        lb01    web01        192.168.119.11        web01web02        192.168.119.12        web02

关闭防火墙

iptables -Fsed  -i 's/enforcing/disabled/' /etc/selinux/configsystemctl stop firewalldsystemctl disable firewalld

一、web01服务器配置nginx,创建index.html

server {        listen       80;        server_name  192.168.119.11;        location / {        root /node;            index  index.html index.htm;        }} mkdir /node echo 'i am web01' > /node/index.html #启动NGINX ./sbgin/nginx

二、web01服务器配置nginx,创建index.html

server {    listen       80;    server_name  192.168.119.12;    location / {        root /node;        index  index.html index.htm;} mkdir /node echo 'i am web02...' > /node/index.html #启动nginx ./sbing/nginx

三、配置lb01服务器的nginx负载均衡

1.检查lb01的 nginx.conf

http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    upstream node {      server 192.168.119.11:80;      server 192.168.119.12:80;}    server {        listen       80;        server_name 192.168.119.10;        location / {          proxy_pass http://node;          include proxy_params;  #需要手动创建        }    }}

2.手动创建proxy_params文件,文件中存放代理的请求头相关参数

[root@lb01 conf]# cat /opt/nginx/conf/proxy_paramsproxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 30;proxy_send_timeout 60;proxy_read_timeout 60;proxy_buffering on;proxy_buffer_size 32k;proxy_buffers 4 128k;
启动lb01负载均衡nginx服务./sbin/nginx

四、访问lb01节点nginx,反复刷新

五、nginx负载均衡调度算法

调度算法      概述轮询        按时间顺序逐一分配到不同的后端服务器(默认)weight       加权轮询,weight值越大,分配到的访问几率越高ip_hash      每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器url_hash      按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器least_conn    最少链接数,那个机器链接数少就分发

1.轮询(不做配置,默认轮询)

2.weight权重(优先级)

3.ip_hash配置,根据客户端ip哈希分配,不能和weight一起用

六、nginx动静分离负载均衡

 

环境准备 

 

系统                 服务                软件                ip地址centos7(lb01)                负载均衡            nginx proxy        192.168.119.10centos7(web01)                静态资源            nginx静态资源        192.168.119.11centos7(web02)                动态资源            django            192.168.119.1

一、在web01机器上,配置静态资源,图片等

cat nginx.confserver {        listen       80;        server_name  192.168.119.11;        #定义网页根目录         root /code;        #定义了静态资源        index index.html;#域名匹配,所有的png、jpg、gif请求资源,都去/root/code/images底下找         location ~* .*\.(png|jpg|gif)$ {                root /code/images;        }    #重启nginx ./sbin/nginx
#创建目录mkdir -p /code/images#准备首页文件[root@web01  /code]$cat index.htmlstatic files...#准备静态文件,图片[root@web01  /code/images]$wget http://pythonav.cn/av/girlone.jpg^C[root@web01  /code/images]$lsgirlone.jpg

二、在web02配置动态请求,准备一个flask程序和静态资源转发

cat  nginx.conf#静态资源地址upstream static {server 192.168.119.11:80;} #flask动态请求upstream flask {server 192.168.119.12:8080;}     server {        listen       80;        server_name  192.168.119.12;       #当请求到达192.168.119.12:80/时,转发给flask的8080应用        location / {            proxy_pass http://flask;            include proxy_params;        }       #当判断资源请求是 192.168.119.12/girl.jpg时候,转发请求给static地址池的服务器192.168.119.11/        location ~ .*\.(png|jpg|gif)$ {        proxy_pass http://static;include proxy_params;}

准备flask应用,flask.py

from flask import Flaskapp=Flask(__name__)@app.route('/')def hello():    return "i am flask....from nginx"if __name__=="__main__":    app.run(host='0.0.0.0',port=8080)

后台运行flask程序

python flask-web.py &

三、在负载均衡服务器lb01上测试访问192.168.119.10

参考:

总结:

1.yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了    cd /opt/    yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y2.安装配置nginx软件,下载源代码    wget -c https://nginx.org/download/nginx-1.12.0.tar.gz3.解压缩源码,编译且安装    tar -zxvf nginx-1.12.0.tar.gz     切换源码目录    ./configure --prefix=/opt/nginx112/    make && make install 4.进入nginx的工作目录cd /opt/nginx112/5.查看gninx的工作目录[root@localhost nginx112]# lsconf  配置文件目录html  网页根目录,你的index.html就放在这里,然后通过域名访问  pythonav.cn/index.html     html/index.html logs    日志sbin    存放nginx可执行命令的6.定制自己的nginx网站修改/opt/nginx112/html/index.html  这是nginx网页根文件,清空内容写入自己的html标签7.启动nginx服务器/opt/nginx112/sbin/nginx    直接回车执行 8.检查nginx服务端口ps -ef|grep nginx 9.通过windows访问nginx web服务浏览器 访问http://192.168.13.79

 

nginx1.编译安装配置完成/opt/nginx11/html/index.html   这是网页的首页文件2.    nginx.conf主配置文件学习######################################如下worker_processes  4;   nginx工作进程数,根据cpu的核数定义events {    worker_connections  1024;    #连接数}#http区域块,定义nginx的核心web功能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;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    #保持长连接    keepalive_timeout  65;    #支持图片 gif等等压缩,减少网络带宽    gzip  on;        #这个server标签 控制着nginx的虚拟主机(web站点)    server {        # 定义nginx的入口端口是80端口        listen       80;        # 填写域名,没有域名就写ip地址        server_name  www.s15rihan.com;        # 定义编码        charset utf-8;        # location定义网页的访问url        #就代表 用户的请求 是  192.168.13.79/        location / {            #root参数定义网页根目录            root   html;            #定义网页的首页文件,的名字的            index  index.html index.htm;        }        #定义错误页面,客户端的错误,就会返回40x系列错误码        error_page  404  403 401 400            /404.html;        #500系列错误代表后端代码出错        error_page   500 502 503 504  /50x.html;    }    #在另一个server{}的外面,写入新的虚拟主机2    server{        listen 80;        server_name  www.s15oumei.com;        location /  {        root  /opt/myserver/oumei;        #定义虚拟主机的网页根目录        index  index.html;        }    }}3.准备两个虚拟主机的网页根目录内容    [root@localhost myserver]# tree /opt/myserver/        /opt/myserver/        ├── oumei        │   └── index.html        写入自己的内容        └── rihan            └── index.html        写入自己的内容         4.修改windows本地的测试域名  C:\Windows\System32\drivers\etc\hosts文件写入如下内容192.168.13.79 www.s15rihan.com  192.168.13.79 www.s15oumei.com      因为我们没有www.s15oumei.com 也没有  www.s15rihan.com ,因此要在本地搞一个测试域名,    不想改dns的话,就去阿里云去买一个域名~~~~~~~~~~~~~~~~~~~~~~    5.然后在浏览器测试访问 两个不同的 web站点www.s15rihan.com  www.s15oumei.com   nginx的访问日志功能1.开启nginx.conf中的日志参数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;        2.检查access.log的日志信息tail -f  access.log nginx的拒绝访问功能1.在nginx.conf中,添加参数在server{}虚拟主机标签中,找到location 然后添加参数        #当赵一宁访问  192.168.13.79/  的时候         location / {            #拒绝参数是 deny             #deny 写你想拒绝的IP地址            #deny还支持拒绝一整个网站            deny  192.168.13.33;            root   /opt/myserver/rihan;            index  index.html;        }nginx的错误页面优化1.修改nginx.conf 中的配置参数这个s1540x.html存在 虚拟主机定义的网页根目录下  error_page  404              /s1540x.html; vpn就是正向代理中国的用户,在自己机器上,使用了一个vpn的ip地址,然后通过这个vpn的IP地址和外接通信nginx的反向代理功能(自带了反向代理的功能,天生的二道贩子)1.实验环境准备准备2个服务器,都安装好nginx软件    nginx1        192.168.13.79   作为web服务器 (理解为火车票售票点)    nginx2        192.168.13.24    作为反向代理服务器        (黄牛)        用户   通过浏览器去访问   黄牛 (代理)    浏览器 访问  192.168.13.24    >        192.168.13.792.在反向代理服务器中添加配置nginx负载均衡集群的概念:一堆服务器做一件事1.实验准备准备三台计算机 nginx1      192.168.13.121   作为nginx负载均衡器                只要我访问这个负载均衡器,查看页面的结果,到底是来自于nginx2      192.168.13.24    web服务,提供一个页面        nginx3         192.168.13.79  web服务,提供一个页面 2.先配置两个nginx  web页面      192.168.13.24  准备一个   index.html  写入  你好,我是192.168.13.24机器    192.168.13.79    准备一个    index.html 写入        老了老弟,我是192.168.13.79        然后启动两个nginx web 服务    3.准备一个nginx负载均衡器  192.168.13.121机器上,修改nginx.conf 写入如下内容             定义一个负载均衡池,负载均衡的算法有            调度算法      概述            轮询        按时间顺序逐一分配到不同的后端服务器(默认)            weight       加权轮询,weight值越大,分配到的访问几率越高            ip_hash      每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器            url_hash      按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器            least_conn    最少链接数,那个机器链接数少就分发            1.轮询(不做配置,默认轮询)            2.weight权重(优先级)            3.ip_hash配置,根据客户端ip哈希分配,不能和weight一起用upstream s15webserver  {ip_hash;server 192.168.13.79 ;server 192.168.13.24 ;} 然后在虚拟主机中添加 反向代理配置,将用户的请求,直接转发给 负载均衡池中的服务器server {        listen       80;        #当我的请求来自于 192.168.13.121时,走这>个虚拟主机        server_name  192.168.13.121;        #charset koi8-r;        #access_log  logs/host.access.log  main;        #核心配置,就在这,一条proxy_psss参数即可        location / {          proxy_pass http://s15webserver;            #root   html;            #index  index.html index.htm;        }}4.启动负载均衡器的 nginx服务 5.在客户端windows中测试访问,负载均衡器  192.168.13.121 ,查看请求分发的结果

 

转载地址:https://www.cnblogs.com/chenxi67/p/10218479.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:linux的redis的安装和使用
下一篇:SQlAlchemy的增删改查

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月15日 04时15分54秒