1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > linux搭建一个配置简单的nginx反向代理服务器 2个tomcat

linux搭建一个配置简单的nginx反向代理服务器 2个tomcat

时间:2019-12-07 18:56:20

相关推荐

linux搭建一个配置简单的nginx反向代理服务器 2个tomcat

1.我们只要实现访问nginx服务器能跳转到不同的服务器即可,我本地测试是这样的,

在nginx服务器里面搭建了2个tomcat,2个tomcat端口分别是8080和8081,当我输入我nginx服务器ip的时候它会跳转到8080或者8081端口,实现一个入口对应多台web服务器的功能,在大型项目中,web服务器是多台的,当某一台服务器挂掉的时候,程序还能够正常的运行,提升系统的健壮性和用户体验。

安装配置nginx服务器在我的博客里面已经存在了,nginx配置的说明也有,现在简单的配置一下给大家体验下。

nginx配置文件中需要配置一个hostname就是本机的名称

我们改一下 命令如下

hostname (这台命令是改了本机名称会立刻生效,或者在您的etc/hosts文件夹里面修改也可以,不过我上次改了一下hosts文件夹里面的信息,没有立刻生效,故而用这个命令)

#这个是我初次搭建nginx服务器的配置文件 一个很简单的配置文件

#

user root root;

#cpu的核心数 看cpu的核心数可以执行一下命令

# grep 'core id' /proc/cpuinfo | sort -u | wc -lworker_processes 1;#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]error_log logs/error.log info;

#进程文件pid logs/nginx.pid;#工作模式与连接数上限

events {use epoll;

#单个进程最大连接数(最大连接数=连接数*进程数)配置成linxu打开最大文件数量,看linux打开最大文件数命令 ulimi -nworker_connections 65535;}#设定http服务器http {

#文件扩展名与文件类型映射表include mime.types;

#默认文件类型default_type application/octet-stream;#log_format main '$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;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,

#可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。#防止网络阻塞

#tcp_nopushon;#keepalive_timeout 0;keepalive_timeout 65;#长连接超时时间,单位是秒#gzip on;

#这里的就是我的本机机器的名称而不是ip

#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。upstream { server 120.25.153.204:8080 weight=3;#tomcat 8080端口 server 120.25.153.204:8081 weight=1;#tomcat 8081端口 } server {listen 80;#监听端口server_name ;#域名可以有多个,用空格隔开charset utf-8;#编码格式#access_log logs/host.access.log main;#对 "/" 启用反向代理location / {proxy_pass ;# 必须要加 http 开头,访问的就是此地址proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #log_format '$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 ;#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 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 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificatecert.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;# }#}}

以上配置即可,我测试过可以的,访问同一个地址的时候有时候会跳转到我的8080端口有的会跳转到8081端口

我的服务器ip是120.25.153.204

2个图片输入的ip都是一样的可是显示的结果不一样,做的不一样是为了区别是否能够访问多个web服务器,

通过nginx反向代理跳转到不同的端口,也可以跳转到 不同ip或者相同ip/相同或者不同的端口

注意:当我关闭任意一个端口服务的时候,我输入ip的时候不会照成地址访问不了的情况。

[root@iZ94j7ahvuvZ conf]# ps -ef|grep tomcatroot76661 0 13:56 pts/0 00:00:07 /usr/local/java/jdk1.7.0/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/tomcat-8080/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/tomcat-8080/endorsed -classpath /usr/local/tomcat/tomcat-8080/bin/bootstrap.jar:/usr/local/tomcat/tomcat-8080/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat/tomcat-8080 -Dcatalina.home=/usr/local/tomcat/tomcat-8080 -Djava.io.tmpdir=/usr/local/tomcat/tomcat-8080/temp org.apache.catalina.startup.Bootstrap startroot77201 0 13:58 pts/0 00:00:08 /usr/local/java/jdk1.7.0/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/tomcat-8081/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/tomcat-8081/endorsed -classpath /usr/local/tomcat/tomcat-8081/bin/bootstrap.jar:/usr/local/tomcat/tomcat-8081/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat/tomcat-8081 -Dcatalina.home=/usr/local/tomcat/tomcat-8081 -Djava.io.tmpdir=/usr/local/tomcat/tomcat-8081/temp org.apache.catalina.startup.Bootstrap startroot7811 2233 0 14:53 pts/0 00:00:00 grep tomcat[root@iZ94j7ahvuvZ conf]# kill 7666

如我kill掉了 7666也就是我的8080端口PID进程,访问ip120.25.153.204还是能够正常访问,提到了用户体验

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。