nginx服务,nginx反向代理

(一)nginx

        同apapche功能一样,基于http协议传输,提供web服务,用于搭建linux的web服务器。

1.安装过程:

(1)安装扩展源epel,有的话可不用安装;

-----yum clean all

-----yum install epel-release -y

(2)安装nginx

----yum install nginx -y

(3)写配置文件(写的配置以分号结尾)

-----vim /etc/nginx/nginx.conf

默认关键配置项:

root  /usr/share/nginx/html  默认根目录

location /  {

}

当location里面配置了root根目录及index时,根目录为location里面的root根目录,没有时是外边的root的根目录,如下:

location  /  {

            root /var/www/html;

            index  index.html;         (index.html为主页)

}

本操作采用默认配置,即根目录为/usr/share/nginx/html

-----mkdir /usr/share/nginx/html

在根目录下建a.txt以作为客户端的访问内容

------- vim /usr/share/nginx/html/a.txt

'' hello,world !''

(4)关闭Linux防火墙和selinux(一般在安装之前就把这步做好,布环境)

-----systemctl stop firewalld     关闭防火墙

-----systemctl  disable firewalld    开机也不启动防火墙

-----systemctl  enable firewalld   开机启动防火墙

关闭selinux     ------vim /etc/selinux/config

设置selinux=disable

------reboot重启

(5)重启nginx(配置有更改软件要读取需重启)

-----systemctl start nginx  启动nginx

-----systemctl  reload nginx   重新加载ngnix

-----systemctl enable nginx  开机启动nginx

-----systemctl status nginx   查看nginx状态

(6)客户端发起测试请求

在客户端的firefox游览器上输入http://(服务器的ip):80/a.txt,看是否可访问到a.txt的内容:“hello,world!”,如下:

http://192.168.72.132:80/a.txt   80为端口号

有问题时会报错:(404等数字叫做状态码)

404 not found  找不到内容      403  客户端没有权限   

400开头的是客户端的问题   500开头的是服务端的问题

2.nginx服务实现的思路:

(1)用户在客户端输入网址:192.168.16.147:80/a.txt。(ip和端口定位服务器nginx软件)---------------------》(2)nginx (nginx读取自己的配置文件)-----------------》(3)nginx.conf-----------》(4)/usr/share/nginx/html 。(从配置文件中找到根目录在哪里)------------------------》(5))/usr/share/nginx/html/a.txt (从根目录找到a.txt) ----------------------》(6)服务端接到http协议包(打TCP头,打TP头,打以太网头传出去)

3.补充:

(1)检查端口号: telnet + 服务器IP

(2)nginx服务log   /var/log/nginx/     access.log     error.log

(3)动态查看log:    tail    -f   /var/log/nginx/access.log

(二)nginx反向代理

        用nginx的反向代理做负载均衡(load  balancer),好比现在有4个nginx web服务器,名称分别为pythonweb,web1,web2,web3,当客户端访问服务器时,pythonweb把收到的访问请求分发给web1,web2和web3。pythonweb就是nginx的反向代理服务器,只分配活,不干活。web1,web2,web3是nginxweb服务器,老实地干着活。

1.nginx反向代理官方说明:

------>http://nginx.org/en

----->documentation  (右侧)

------>Using nginx as HTTP load balancer   (里面有负载均衡的三种算法和配置)

三种算法:

轮循;              最小链接调度(谁目前的惹怒我少就给谁分任务);          以IP地址分发请求(这个IP上次在A机器上,这次就分给A机器)

lb -----负载均衡

2.操作步骤:

(1)进入nginx.org/en官网,找到Using nginx as HTTP load balancer ,看官方配置文档;

(2)以pythonweb虚拟机为反向代理服务器,虚拟机web1,web2,web3作为接收访问请求的服务器,记住这四个虚拟机的IP;

(3) 写配置文件

---------vim /etc/nginx/nginx.conf

a. 在http中添加配置(可以在官网复制好再改一下)

upstream  pythonweb {                                    (upstream定义一组机器,pythonweb为组名)

          serve  192.168.72.130;

          serve  192.168.72.131;

         serve  192.168.72.132;                        (web1,web2.web3的IP)

   }

b.在serve下location中加 proxy_pass http://pythonweb,如下:

 location   /   {

          proxy_pass http://pythonweb

}

(4)分别将web1,web2,web3下/etc/nginx/html下index.html内容改为“web1”,“web2”,“web3”;

(5)在火狐游览器上登陆pythonweb的IP,按F5刷新,每刷新一次,内容有web1变web2变web3,证明生效了!

原文地址:https://www.cnblogs.com/weigege2015/p/7397536.html