编程技术分享平台

网站首页 > 技术教程 正文

Linux邂逅Nginx——上高速篇(linux nginx使用)

xnh888 2024-09-12 22:36:12 技术教程 17 ℃ 0 评论

前情提要:昨天分享了怎么在Linux环境下安装并测试Nginx,今天就让我们一起上高速飙飙车

同样,上高速之前我们需要给我们昨天造的车改造一下引擎,要不怎么能干过别人的了,哈哈……

1、修改nginx.conf配置文件

(1)[root@stoneBoy home]# cd /usr/local/nginx/conf/

(2)[root@stoneBoy conf]# vi nginx.conf 然后进入编辑模式(按【i】),在http模块部分加入如下配置:

nginx.conf配置文件修改

重点是我圈出来的部分!!!文本如下:

...

upstream web1{

server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;

server 127.0.0.1:8082 weight=1 max_fails=2 fail_timeout=30s;

}

server {

listen 80;

server_name localhost;

location / {

#root html;

#index index.html index.htm;

index index.jsp index.html index.htm;

root /usr/local/nginx/html;

proxy_next_upstream http_502 http_504 error timeout invalid_header;

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://web1;

}

...

备注:a. weight=1:权重,nginx命中当前tomcat的概率,数值越大,命中率越高;

b. max_fails=2 心跳最大尝试次数,就是nginx服务器如果去ping 8081服务器,如果有两次都没ping通,它会默认该服务已挂,下次就不会再向它分发请求;

c. fail_timeout=30s 超时时间

(3)其实这样nginx就已经可以实现分发地效果了,为了能更好的区分,我用了两个示例项目,以下为后续操作步骤:

[root@stoneBoy usr]# cd tomcat7_8081/conf/ #进入你所配置tomcat的配置文件目录

[root@stoneBoy conf]# vi server.xml

server.xml配置文件修改

加入 <Context path="" docBase="testnginx1" reloadable="true" debug="0" privileged="true"></Context>,每个tomcat都要配置一下默认路径,否则会默认apache的主页

(4)测试 在浏览器中输入192.16.1.14(这是我服务器的ip,改成各位自己的就行).就可以得要你想要的了

tomcat_8081

tomcat_8082

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表