您的位置:首页 > 运维架构 > 反向代理

如何让多个不同类型的后端网站用一个nginx进行反向代理实际场景分析

2019-11-04 00:00 1691 查看

 

        前段时间公司根据要求需要将聚石塔上服务器从杭州整体迁移到张家口,刚好趁这次机会将这些乱七八糟的服务器做一次梳理和整合,断断续续一个月迁移完

成大概优化掉了1/3的机器,完成之后遇到了一些问题,比如曾今零零散散部署在生产上一些可视化UI:apollo,kibana,grafana,jenkins 等等要么采用80端口,要

么对公开放了其他端口,为了安全,现在不再开放非80之外的公网端口,由于机器少了,80端口不够,这些可视化UI不再能直接访问到了。所以需另寻其他出路。

 

一:用nginx做反向代理

      为了解决这两个问题,自然第一反应想到的就是使用反向代理,我的理想构思下应该是下图这样的。

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info
576
;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;

log_format  main  '$host ----> $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {

ae4
listen       80;
server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

# location = /get {
#     set_unescape_uri $key $arg_key;  # this requires ngx_set_misc
#     redis2_query get $key;
#     redis2_pass 10.105.13.174:6379;
# }

location / {

if ($host = "a.mip.com") {
proxy_pass http://localhost:8001;
}

if ($host = "j.mip.com") {
proxy_pass http://localhost:8002;
}

root   html;
index  index.html index.htm;

}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening
1c3e
on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen       8001;
server_name  somename  alias  another.alias;
location / {
root   html;
index  apollo.html;
}
}

server {
listen       8002;
server_name  somename  alias  another.alias;
location / {
root   html;
index  jenkins.html;
}
}

# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

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

}
View Code  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: