http转发

前言

本文介绍如何配置nginx做http转发

环境准备

  • 通过docker容器,准备一台centos主机安装nginx,准备一台tomcat主机,nginx转发到该主机上。
docker run --name docker1 -itd -p 80:80 -p 81:81  centos:7 bash
docker run --name docker2  -d -it --rm -p 8888:8080  tomcat:8.0
  • 登陆dcoker1安装必要软件yum install -y vim && yum install y net-tools && yum install -y wget

安装配置nginx

  • 安装方法,其中配置更改为./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --with-http_ssl_module --with-http_realip_module --with-http_geoip_module --with-http_sub_module --with-stream --with-http_stub_status_module
  • 更改配置文件;vim /opt/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;

#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 {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream tomcats{
        server 172.17.0.2:8080;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcats;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

添加了upstream,以及在location /中添加了proxy_pass

  • 测试配置 nginx -t,确保提示正确;
  • 重新加载nginx,nginx -s reload

你可能感兴趣的:(http转发)