Use nginx to set up the proxy server

  • 2020-05-15 03:41:30
  • OfStack

nginx can use its reverse proxy function for the realization of load balancing, at the same time can also use the function of its forward agent set up a proxy server, such as in the network environment, run on the machine can connect the network nginx as a proxy server, other machines by setting the machine IP and port to go through its connection to the Internet, this article USES nginx official image, through the following steps can be simple to realize the proxy server.

Step 1: start nginx


[root@devops ~]# docker run -p 8888:8888 --name proxy-nginx -d nginx
c7baab8ea9da0a148aa9bcc1295a54391906f6be94efca7189df23ceecdbf714
[root@devops ~]#

Step 2: set nginx

Into the container

[

[root@devops ~]# docker exec -it proxy-nginx sh

]

update apt-get

[

Install ping/vi/ps: apt-get update; apt-get install procps vim inetutils-ping

]

Set nginx conf

The simplest proxy function can be implemented by adding the following content


  resolver   8.8.8.8;
  server {
    listen 8888;
    location / {
      proxy_pass http://$http_host$request_uri;
    }
  }

The remaining information is confirmed by nginx.conf, which has not been modified


# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid    /var/run/nginx.pid;
events {
  worker_connections 1024;
}
http {
  include    /etc/nginx/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 /var/log/nginx/access.log main;
  sendfile    on;
  #tcp_nopush   on;
  keepalive_timeout 65;
  #gzip on;
  resolver   8.8.8.8;
  server {
    listen 8888;
    location / {
      proxy_pass http://$http_host$request_uri;
    }
  }
  include /etc/nginx/conf.d/*.conf;
}
#

Step 4: set the client

Set the server IP and the above port 8888 on the client side to connect to the network through the proxy server.

conclusion


Related articles: