Nginx分布式服务器搭建

1.配置环境

    Linux Centos7 64位系统。安装gcc-c++编译环境(yum install gcc-c++ -y)。

    安装pcre。https://ftp.pcre.org/pub/pcre/

    tar zxvf pcre-8.32.tar.gz
    cd pcre-8.32
    ./configure
    make && make install 

    安装zlib。 http://www.zlib.net/zlib-1.2.11.tar.gz

    tar zxvf zlib-1.2.11.tar.gz
    cd zlib-1.2.11
    ./configure
    make && make install

    安装openssl。https://www.openssl.org/source/openssl-fips-2.0.16.tar.gz

    tar zxvf openssl-fips-2.0.16.tar.gz
    cd openssl-fips-2.0.16
    ./config
    make && make install
2.Nginx安装

    1)安装nginx。http://nginx.org/download/nginx-1.12.2.tar.gz

    tar zxvf nginx-1.12.2.tar.gz
    cd nginx-1.12.2
    ./configure
    make && make install

    2)nginx安装完后所在位置: /usr/local/nginx

    cd /usr/local/nginx

    3)执行指令启动 sbin/nginx

    sbin/nginx

    4)配置文件 conf/nginx.conf (核心配置文件)

    vi conf/nginx.conf
3.Nginx配置文件编写

    1)全局配置

 # 全局配置
    worker_processes  1;  #工作进程数(和CPU内核数量保持一致)

    error_log  logs/error.log;  # 错误日志配置
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/nginx.pid;

    # 动态配置
    events {
	# 连接数执行
	worker_connections  1024;
    }

    # 对http环境配置
    http {
	# 网站处理数据类型
	include       mime.types;
	# 默认数据传输格式
	default_type  application/octet-stream;
	# 支持文件发送
	sendfile        on;

    # 虚拟主机(网站)
    server {
	# 监听端口号
        listen       80;
        # 服务器主机域名
        server_name  localhost;
        # 网站url映射
	location / {
	    # 静态资源所在位置(相对路径或绝对路径)
	    root   html;
	    # 默认首页
	    index  index.html index.htm;
	}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        # 错误重定向页面指定(跳转url)
	error_page   500 502 503 504  /50x.html;
	# 网站url映射
	location = /50x.html {
	     root   html;
	}
    }
}

※ 生产和开发服务器防火墙配置:关闭selinux、设置firewalld
临时关闭selinux -- setenforce 0
临时关闭firewall -- systemctl stop firewalld
4.Nginx实现反向代理
# 反向代理声明
upstream 自定义名称{
	# 代理的tomcat服务器、或者填写动态云服务器的IP
	server 127.0.0.1:8080;
}

server{
	# 监听端口号
        listen       80;
        # 服务器主机域名
        server_name  localhost;
        # 映射静态资源目录
        root /www/itrap/itrapfront;
        # 映射/的URL
        location /{
        	# 把客户端的主机信息和信息通过nginx传递给后台代理tomcat服务器
        	proxy_set_header Host $host;
        	proxy_set_header X-Real-IP $remote_addr;
        	# 映射反向代理声明
        	proxy_pass http://自定义的名称;
        }
   }
location ~ .*\.(gif|jpg|jpeg|png|bmp|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|wma|woff|woff2|ttf|mp4|htm|html|swf|mp3){
    expires 6h;
}                                *缓存时间设置*
location ~ .*\.(js|css){
    expires 2h;
}
nginx指令:
    检测nginx配置文件内容格式是否正确
    sbin/nginx -t
    ※ 如果发生格式或内容错误,错误信息会动态的显示出来。
    使用新的配置文件重启nginx
    sbin/nginx -s reload
    nginx的upstream中指定负载均衡
    默认设置 -- 轮询
    server 127.0.0.1:8080 weight=20; --权重
    独立一行写ip_hash; -- 通过客户端IP动态分配固定tomcat,长期服务。(多服务器下session持久化问题)
        


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值