当前位置: 首页 - 编程技术 - 文章正文

【网安基础之Linux系统Ⅶ】Nginx服务器

xiaoqihv

一.Nginx服务器:使用基于事件驱动架构,支持超高并发,软件开源,是跨平台服务器

主要功能:1.http服务器(可以发布网站)

2.正向代理服务器

3.反向代理服务器

4.负载均衡

代理:用服务器(其他人的电脑)帮你访问要访问的网页【和召唤美团快递小哥一个道理。餐厅老板和你,你们俩谁都没见过谁。在这个过程中,只有快递小哥知道你家地址,老板并不知道,(不好吃就给个差评,老板也找不到你)是不是很安全】

1.正向代理:代理服务器代替客户端发出请求(为客户机代言,隐藏客户端信息)

2.反向代理:代理服务器和要访问服务器之间(为服务器代言,保证内网安全,客户端只知道代理服务器的ip,不知道目的服务器ip)

Nginx适合用于反向代理

3.负载均衡(让数据分配给哪个服务器更合理,性能好的多收一些数据,性能差的少收一些,硬件负载均衡---贵,软件负载均衡----自己安装)

(1)weight轮询:设置权重

(2)ip_hash哈希:根据不同的ip的hash结果匹配确定访问的服务器

二.安装nginx

1.创建

创建nginx的yum源 vim /etc/yum.repos.d/nginx.repo       //如果yum list nginx没有找到安装文件就创建源

[nginx]

name=nginx_repo

baseurl=http://nginx.org/packages/centos/7/$basearch/

gpgcheck=0

enabled=1

2、yum  list  nginx   //查询是有nginx安装包

3、yum install nginx -y     //安装nginx

4、启动服务并检查是否启动成功

systemctl start nginx

lsof -i:80 查看端口是否已经启动

netstat -antp | grep 80

[root@xxxxxxxx ~]# lsof -i:80

[root@xxxxxxxx ~]# netstat -antp | grep 80

5、浏览器测试(window系统用谷歌浏览器,linux用curl指令)

如果浏览器打不开,关闭防火墙 systemctl  stop firewalld setenforce 0

[root@xxxxxxxx ~]# curl 192.168.47.185

三.关闭版本号(在对应的IP后面输入一个错误的目标:192.168.111.130/aaa

会出现错误提示,并且会暴露自己的nginx版本号,这是不安全的,需要关闭nginx错误提示版本号)

方法:(1)修改配置文件:  vim /etc/nginx/nginx.conf 在tcp_nodelay on;后面(或前面)添加server_tokens off;

  (2)nginx -s reload   //热启动nginx

四.Nginx的相关文件

1.主配置文件:  vim /etc/nginx/nginx.conf    //在此文件中修改相关信息,以及配置负载均衡等

[root@xxxxxxxx ~]# cat /etc/nginx/nginx.conf

2.默认主页目录:/usr/share/nginx/html/idex.html    //在此目录下创建新主页即可搭建新网站

[root@xxxxxxxx ~]# cat /usr/share/nginx/html/index.html

3.etc/nginx/conf.d/default.conf         //默认主页中定义了监听 端口,日志信息,错误页面,主页页面等

五.Nginx调用php

Apcahe和Nginx区别:nginx和php相互是独立的,nginx安装PHP需要一个调节器:php-fpm 才能让nginx使用PHP。Apcahe中集成了php

1.安装php和php-fpm

(1)安装PHP

yum install php -y

[root@xxxxxxxx ~]# yum install php -y

(2)编辑php主页

vim  /usr/share/nginx/html/index.php 编辑内容<?php phpinfo();?>

[root@xxxxxxxx ~]# vi /usr/share/nginx/html/index.php

通过浏览器访问发现无法调用php页面,说明缺少php-fpm模块

(3)安装php-fpm

yum install php-fpm -y

开启php-fpm应用:systemctl start  php-fpm

php-fpm默认端口为9000,可以使用lsof -i:9000 查看是否开启。

[root@xxxxxxxx ~]# lsof -i:9000

(4)修改nginx的配置文件

vim /etc/nginx/conf.d/default.conf

第一步:location / {

           root   /usr/share/nginx/html;

           index  index.html index.htm index.php;//添加index.php

}

第二步:将使用php的配置取消掉原来的注释:

location ~ \.php$ {

        root           /usr/share/nginx/html;//改成网站根目录

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;//将/scripts改成$document_root

        include        fastcgi_params;

(5)重启服务:systemctl reload nginx

(6)将/usr/share/nginx/html/index.html  名字更换,以免和index.php冲突 mv  index.html index.tys

(7)去浏览器访问IP,出现PHP的信息页面就说明nginx调用PHP成功了

2.配置nginx虚拟主机

方法一:

基于端口

进入配置目录下:cd  /etc/nginx/conf.d/

新建两个配置文件:

vi web1.conf

server  {

      listen 800;----------监听端口为800

      root    /usr/share/nginx/web1;---------网站根目录

}

vi web2.conf

server  {

      listen 8000;----------监听端口为8000

      root    /usr/share/nginx/web2;---------网站根目录

}

重启或者热部署nginx服务:

nginx -s reload    热启动nginx

systemctl restart nginx 重启

浏览器输入ip加端口测试:192.168.111.130:800  /  192.168.111.130:8000

方法二:

基于域名

vi web1.conf

server  {

    listen 80;----------监听端口为80

    server_name www.tys1.com;----------网站对应域名

    root    /usr/share/nginx/web1;---------网站根目录

    }

vi web2.conf

server  {

    listen 80;----------监听端口为80

    server_name www.tys2.com;----------网站对应域名

    root    /usr/share/nginx/web2;---------网站根目录

    }

修改hosts文件:定义设置的两个域名的IP都为本台服务器的IP

    vi /etc/hosts

添加:

192.168.111.130  www.tys1.com

192.168.111.130  www.tys1.com

重启或者热部署nginx服务:

nginx -s reload    热启动nginx

systemctl restart nginx 重启

只能在linux系统中进行访问:

curl www.tys1.com

curl www.tys2.com

方法三:

基于IP

首先虚拟出两个IP:

ifconfig ens33:1  192.168.111.200

ifconfig ens33:2  192.168.111.201

vi web1.conf

server  {

    listen  192.168.111.200 ;----------监听ip为192.168.111.200

    root    /usr/share/nginx/web1;---------网站根目录

}

vi web2.conf

server  {

    listen  192.168.111.201 ;----------监听ip为192.168.111.201

    root    /usr/share/nginx/web2;---------网站根目录

}

重启或者热部署nginx服务:

nginx -s reload    热启动nginx

systemctl restart nginx 重启

去浏览器访问这两个IP。

六.Nginx反向代理

实现:代理一台web服务器需要两台系统,一台当反向代理服务器,一台当web服务器。

第一步:浏览器测试访问服务器ip

第二步:

修改配置文件: vim /etc/nginx/conf.d/default.conf

server {

 location / {

     #   root   /usr/share/nginx/html;     #改为注释//一定要注释

          proxy_pass http://192.168.111.200;    #添加,代理去往哪台web服务器,web服务器的IP

     #   index  index.html index.htm index.php;  //可注释可不注释

    }

}

第三步:重启服务:nginx -s reload

第四步:浏览器测试,去浏览器访问反向代理服务器的IP,会发现能够访问到另一台web服务器的主页

七.反向代理和负载均衡(代理多台web服务器:首先得有多个网站,需要在web服务器里面实现一主机多网站。记得关闭防火墙。)

(1)先搭建2个apcahe服务器,通过虚拟主机实现

ifconfig ens33:1 192.168.1.205/24

ifconfig ens33:2 192.168.1.206/24

mkdir /var/www/web205

mkdir /var/www/web206

vim /var/www/web205/index.html

vim /var/www/web206/index.html

(2)创建虚拟主机对应的子配置文件

vim /etc/httpd/conf.d/201.conf----此时在这里不需要去配置主配置文件,可以添加两个自配置文件

vim /etc/httpd/conf.d/202.conf

(3)重启httpd服务并测试

systemctl httpd restart

systemctl  stop firewalld

setenforce 0

用浏览器访问两个ip看下是否页面正常

(4)nginx代理配置

vim /etc/nginx/conf.d/default.conf

upstream www.apple.com {

server 192.168.1.205 ; weight=1;  #添加自己的web服务器的IP。设置负载均衡,权重为1

server 192.168.1.206; weight=5;  #添加自己的web服务器的IP。设置负载均衡,权重为5

}

server {

listen       80;

server_name  localhost; location / {

注意:在这里可以添加ip_hash;或者权重等负载均衡参数

# root   /usr/share/nginx/html;    注意:务必注加释调

proxy_pass Apple;

index  index.html index.htm index.php;

}

(5)浏览器测试

八.squid正向代理服务器

1.关闭服务器

2.安装squid:yum install -y squid

3.编辑squid配置文件:vim /etc/squid/squid.conf

在配置文件内加:

acl local    src 0.0.0.0/0

http_access allow local

4.启动squid服务:systemctl start start【squid默认端口号是:3128】

九.Windows10上的squid正向代理测试

十.Linux上的squid正向代理测试

(1)在部署squid代理的服务器外的其他机子上

echo "export http_proxy=http://192.168.111.130:3128" >>/etc/profile   //开启http代理

echo "export https_proxy=http://192.168.111.130:3128" >>/etc/profile //开启https代理

相当于: vi /etc/profile

添加:export http_proxy=http://192.168.111.130:3128

      export https_proxy=http://192.168.111.130:3128

      source /etc/profile    // /etc/profile为系统环境变量,系统启动生效

使用:curl www.baidu.com 进行测试

文章地址:https://wenmayi.cn/post/120.html