nginx implements reverse proxy and load balancing

不言
Release: 2023-04-02 20:26:02
Original
1594 people have browsed it

This article mainly introduces the implementation of reverse proxy and load balancing by nginx. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Because it is simulated in a stand-alone environment Multiple server environment, so multiple tomcats open different ports to achieve

1. Reverse proxy

Goal: (When no port number is added, port 80 is accessed by default, so in one To achieve access to two domain names on a server, a reverse proxy is required)

1. Create tomcats directory

mkdir /usr/local/tomcats
tar -zxf apache-tomcat-7.0.47.tar.gz 
mv apache-tomcat-7.0.47 /usr/local/tomcats/tomcat1
cp -r tomcat1/ tomcat2
...
Copy after login

2. Modify the port

Involves three ports Modification: HTTP default port 8080 Remote server port 8005 AJP port 8009

vim conf/server.xml 
分别修改为8081  8006  8009
...
Copy after login

3. Simulate access to different domain names by modifying the host

这里有个小工具switchhsot可以方便的进行修改
(将ip+端口   分别与域名关联)
Copy after login

4. Modify nginx configuration

cd /usr/local/nginx/conf
vim nginx.conf

 upstream tomcatserver1{

server 47.94.14.93:8081;

   }
 upstream tomcatserver2{

    server 47.94.14.93:8082;

   } 
   server {
    listen       80;
    server_name  8081.kaka.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        proxy_pass http://tomcatserver1;
        index  index.html index.htm;
    }
}
  server {
    listen       80;
    server_name  8082.kaka.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        proxy_pass http://tomcatserver2;
        index  index.html index.htm;
    }
}
Copy after login

5. Visit the domain name set earlier and find that you can all access

2. Load balancing

  1. Under the same domain name, one server can no longer meet our needs. You need multiple servers to distribute multiple requests to different servers to reduce the pressure on the server. At this time, you need load balancing

##2. Configuration:

    (1)只需要在上面的反向代理的upstream中添加相应的server即可实现平均分配
Copy after login

nginx implements reverse proxy and load balancing

    (2)有时候我们负载均衡服务器的性能不同,所以分配请求的权重不同
Copy after login

nginx implements reverse proxy and load balancing

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Use nginx proxy to support WeChat web page authorization for different domain names

The above is the detailed content of nginx implements reverse proxy and load balancing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!