Nginx https反向代理和负载均衡服务器配置(centos)

来源:互联网 发布:网络自制剧排行榜 编辑:程序博客网 时间:2024/06/05 06:14

Nginx https反向代理和负载均衡服务器配置(centos

Nginx安装包下载

   1pcre

    http://www.pcre.org/

    2nginx

http://nginx.org/

下载mainline version的版本,本次下载了 nginx-1.13.5

Nginx安装

 

1、安装linux常用库

yum install -y gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid mysql-devel

2上传pcre2-10.21.tar.gz, nginx-1.13.5.tar.gz 到 /usr/local/src/nginx目录

3解压pcre2-10.21.tar.gz
    # cd /usr/local/src/nginx
    # tar zxvf pcre2-10.21.tar.gz
   
    4)进入解压后的目录
    # cd pcre2-10.21
   
    5)配置
    #  ./configure
    6) 编译
    #  make
    7) 安装
    #  make install   

3.Nginx安装
    创建安装目录与日志目录

1) 创建用户nginx使用的test用户。
    # groupadd  test #添加test组    
    # useradd -g test test-s /bin/false  #创建nginx运行账户test并加入到test组,不允许test用户直接登录系统
    2 安装目录
    # mkdir /usr/local/nginx
    3) 日志目录
    # mkdir /data0/logs/nginx

# chown test:test/data0/logs/nginx -R
   
    4) 判断系统是否安装了zlib-devel。如果没有安装。使用
    # yum install -y zlib-devel
   
    5) 解压
    # cd /usr/local/src/nginx
    # tar zxvf nginx-1.13.5.tar.gz
   
    6) 进入目录
    # cd nginx-1.13.5
   
    7) 配置。通常将软件安装在/usr/local/目录下。
    # ./configure --user=test --group=test --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module


    8)编译
    # make
   
    9)  安装
    #  make install
   
    10)  检查是否安装成功
    # cd  /usr/local/nginx/sbin
    # ./nginx -t
    结果显示:
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

   表示安装成功

上传服务器证书

mkdir /usr/local/nginx/conf/ssl

上传服务器ssl证书到 mkdir /usr/local/nginx/conf/ssl目录


配置https反向代理配置和http负载

配置/usr/local/nginx/conf/nginx.conf

#设置低权限用户,为了安全而设置的

user test test;

#工作衍生进程数

worker_processes 4;

#设置错误文件存放路径

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

#设置pid存放路径(pid是控制系统中重要文件)

#pid logs/nginx.pid;

#设置最大连接数

events{

    worker_connections 1024;

}

http{

  #设置负载服务器

    upstream www.test.com{

        server 192.168.18.8:80;

        server 192.168.18.9:80;

    }

  #设置https反向代理

    server {

        listen 443;

        ssl on;

        server_name  www.test.com; # 设置你的http服务器地址

        access_log logs/ssl-access.log;

        error_log logs/ssl-error.log debug;

        ssl_certificate      ssl/server.crt;

        ssl_certificate_key  ssl/server.key;

        keepalive_timeout 60;

proxy_connect_timeout 300;

proxy_read_timeout 300;

proxy_send_timeout 300;

        location / {

            proxy_pass http://www.test.com;

    proxy_set_header Host $http_host;

            #proxy_redirect default;

    client_max_body_size    5120m;

#      proxy_temp_file_write_size 0m;

    proxy_request_buffering off;

       }

    }

}

启动nginx

1测试配置文件是否正确

/usr/local/nginx/sbin/nginx -t -c /path/to/nginx.conf

测试成功后启动nginx

2启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

3如有更改nginx.conf 则重新reload

/usr/local/nginx/sbin/nginx -s reload 

4关闭nginx 

/usr/local/nginx/sbin/nginx -s stop


阅读全文
0 0