使用nginx搭建反向代理服务器

深度链接 / 2023-12-06 21:48:19 / 112

nginx是一个轻量级、高性能的http和反向代理服务器。其特点是占用内存小,支持高并发(采用了多进程和异步机制【异步非阻塞】),支持负载均衡等。

1、Nginx特点

#是一个支持高并发、高性能、轻量级的http服务器

#可以作为邮件代理服务器

#可以作为反向代理服务器

#支持负载均衡,可以作为负载均衡服务器

高性能、高并发、轻量级、占用内存小、模块化


2、系统环境

[root@wrx ~]# uname -sir
Linux 2.6.32-696.el6.x86_64 x86_64


3、搭建反向代理服务器

新建 Microsoft Visio 绘图.jpg

3.1、基本环境搭建

新建三个虚拟机,系统都安装了CentOS6.9 64位(系统安装及基本实验环境搭建略过)。将192.168.1.171作为反向代理服务器,其余两台作为web服务器。搭建结构如上图所示。


3.2、web服务器安装

在192.168.1.118和192.168.1.169上安装Apache并开启相应服务,然后在web根目录下新建index.html文件,文件内容为其ip地址。

[root@wrx ~]# yum install httpd -y

[root@wrx ~]# service httpd start


3.3、安装Nginx

在192.167.1.171上安装nginx

#安装gcc
[root@wrx ~]# yum install gcc-c++ -y

#安装nginx需要的依赖库
[root@wrx ~]# yum install zlib-devel openssl-devel pcre-devel -y

#查询是否有安装过nginx
[root@wrx ~]# rpm -qa | grep nginx
#或
[root@wrx ~]# whereis nginx

#安装nginx
[root@wrx ~]# yum install nginx -y

#查看是否安装
[root@wrx ~]# rpm -qa | grep nginx
nginx-mod-mail-1.10.2-1.el6.x86_64
nginx-mod-stream-1.10.2-1.el6.x86_64
nginx-mod-http-perl-1.10.2-1.el6.x86_64
nginx-mod-http-geoip-1.10.2-1.el6.x86_64
nginx-filesystem-1.10.2-1.el6.noarch
nginx-mod-http-image-filter-1.10.2-1.el6.x86_64
nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64
nginx-1.10.2-1.el6.x86_64
nginx-all-modules-1.10.2-1.el6.noarch

#或
[root@wrx ~]# whereis nginx

#查看nginx版本
[root@wrx nginx]# nginx -v
nginx version: nginx/1.10.2

#修改nginx配置
[root@wrx data]# cd
[root@wrx ~]# cd /etc/nginx/
[root@wrx nginx]# ls
conf.d  default.d  fastcgi.conf  fastcgi.conf.default  fastcgi_params  fastcgi_params.default  koi-utf  koi-win  mime.types  mime.types.default  nginx.conf  nginx.conf.default  scgi_params  scgi_params.default  uwsgi_params  uwsgi_params.default  win-utf
[root@wrx nginx]# cp nginx.conf nginx.conf.bak
[root@wrx nginx]# vim nginx.conf
#nginx.conf文件内容如下
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections  1024;
}


http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

        upstream server_pool {
                server 192.168.1.118:80;
                server 192.168.1.169:80;
        }
        server {
                #监听80端口
                listen 80;
                #服务器名称
                server_name www.iproxy-test.com;
                #编码格式
                charset utf8;
                location / {
                        #如果是搭建负载均衡服务器,则此处为负载名称;如果是反向代理服务器,则此处配置需要代理的url
                        proxy_pass  http://server_pool;
                }
        }
}

#查看nginx服务状态
[root@wrx nginx]# service nginx status
nginx is stopped

#开启nginx服务
[root@wrx nginx]# service nginx start
Starting nginx:                                            [  OK  ]



附:

使用yum安装nginx时出现如下问题

warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6


GPG key retrieval failed: [Errno 14] Could not open/read


解决办法

#首先查看/etc/pki/rmp-gpg目录是否存在RPM-GPG-KEY-EPEL-6文件
[root@wrx ~]# ls /etc/pki/rmp-gpg
ls: cannot access /etc/pki/rmp-gpg: No such file or directory

#如果不存在
[root@wrx ~]# wget -O /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 https://fedoraproject.org/static/0608B895.txt

#导入密钥密令
[root@wrx ~]# rpm --import /etc/pki/rpm-gpg/RPM*

#再次使用yum命令安装nginx试试看