Home >Operation and Maintenance >Nginx >How to implement nginx reverse proxy in Centos7

How to implement nginx reverse proxy in Centos7

WBOY
WBOYforward
2023-06-02 20:16:011136browse

Reverse proxy server is set up on the server side to alleviate the workload of the server by buffering frequently requested pages and forward client requests to the target server on the internal network; and The results obtained from the server are returned to the client requesting a connection on the Internet. At this time, the proxy server and the target host appear as a server to the outside world. Currently, web websites use reverse proxies, which in addition to preventing vicious attacks from the external network on internal servers, caching to reduce server pressure and access security control.

How to implement nginx reverse proxy in Centos7

Experimental environment:

192.168.1.188 nginx load balancer

192.168.1.189 web01 server

192.168. 1.190 web02 server

Software preparation:

centos7.4 x86_64

nginx-1.6.3.tar.gz

Install nginx software

Install dependent software package command collection
[root@localhost ~]# yum -y install openssl openssl-devel pcre pcre-devel gcc
Install nginx software package command collection
[root@localhost ~]# mkdir /app[root@localhost ~]# cd /app[root@localhost ~]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz[root@localhost ~]# useradd -s /sbin/nologin -M[root@localhost ~]# tar xf nginx-1.6.3.tar.gz[root@localhost ~]# cd nginx-1.6.3[root@localhost ~]# ./configure --user=nginx --group=nginx --prefix=/app/nginx --with-http_stub_status_module --with-http_ssl_module[root@localhost ~]# make && make install
Configuration file

(below The operation is performed on web01 and web02)

[root@localhost ~]# vim /app/nginx/conf/nginx.conf

Modify the configuration file to the following content

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "http_referer" ''"$http_user_agent" " $http_x_forwarded_for"';
server {
listen 80;
server_name bbs.dengchuanghai.org;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/access_bbs.log main;
}

}
[root@localhost ~]# mkdir /app/nginx/html/bbs[root@localhost ~]# echo "192.168.1.189 bbs" >>/app/nginx/html/bbs/index.html            [root@localhost ~]# echo "192.168.1.189 bbs.dengchuanghai.org" >> /etc/hosts                    [root@localhost ~]# echo "192.168.1.190 bbs" >>/app/nginx/html/bbs/index.html  [root@localhost ~]# echo "192.168.1.190 bbs.dengchuanghai.org" >> /etc/hosts

(Enter the above content on the two web servers respectively)

Then start them respectively nginx

[root@localhost ~]# /app/nginx/sbin/nginx -t (检查配置文件有无错误)[root@localhost ~]# /app/nginx/sbin/nginx  启动[root@localhost ~]# ss -tnlp | grep 80

Use curl bbs.dengch How to implement nginx reverse proxy in Centos7, the following operations are performed on the nginx load balancer

[root@localhost ~]# vim /app/nginx/conf/nginx.conf

Change to the following

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools{
server 192.168.1.190:80 weight=1;
server 192.168.1.189:80 weight=1;
}
server {
listen 80;
server_name www.dengchuanghai.org;
location / {
proxy_pass http://www_server_pools;
}
}
}
[root@localhost ~]# echo "192.168.1.188 www.dengchuanghai,org" >> /etc/hosts

Check the syntax

[root@localhost ~]# /app/nginx/sbin/nginx -t

Start the service

[root@localhost ~]# /app/nginx/sbin/nginx

How to implement nginx reverse proxy in Centos7 Use curl www.dengchuanghai.org How to implement nginx reverse proxy in Centos7 The result output is found to be output by two servers in turn

The above is the detailed content of How to implement nginx reverse proxy in Centos7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete