OpenResty配置多域名反向代理

今天介绍一下单台服务器上面,配置多台子系统的反向代理配置(域名访问)。下面看一下详细的配置。

nginx.conf配置

#user  nobody;
worker_processes  1;

error_log  logs/error.log  notice;
pid        logs/nginx.pid;
worker_rlimit_nofile 65535;

events {
	use epoll;
    worker_connections  65535;
}

env BLOGAPP_PATH;

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    include /home/nginx/config/conf.d/reverse-proxy.conf;

    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    client_max_body_size 50m; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
    client_body_buffer_size 256k;
	client_header_timeout 3m;
	client_body_timeout 3m;
	send_timeout 3m;

	proxy_connect_timeout 300s; #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_read_timeout 300s; #连接成功后,后端服务器响应时间(代理接收超时)
    proxy_send_timeout 300s;
    proxy_buffer_size 64k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
    proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
    proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
    proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
    proxy_ignore_client_abort on; #不允许代理端主动关闭连接

    lua_shared_dict configCache 10m;

    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        location / {
            root html;
            index index.html index.htm;
        }

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

    }

}

上面的配置OpenResty和Nginx的配置是类似的,上面引用了一个reverse-proxy.conf文件,里面是配置了详细的代理的信息。

reverse-proxy.conf配置

server {
    listen 80;
    server_name cas.moguhu.com;
    location / {
        proxy_redirect off;
        proxy_set_header host $host;
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8080;
        # proxy_pass http://192.168.10.40:9100; # 如果是转发到其它主机
    }
    access_log /var/logs/nginx/cas.moguhu.com_access.log;
}

server {
    listen 80;
    server_name uc.moguhu.com;
    location / {
        proxy_redirect off;
        proxy_set_header host $host;
        proxy_set_header x-real-ip $remote_addr;
        proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8081;
        # proxy_pass http://192.168.10.50:9101; # 如果是转发到其它主机
    }
    access_log /var/logs/nginx/uc.moguhu.com_access.log;
}

通过上面的配置就可以完成对后端服务的反向代理了。