Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build reverse proxy for Nginx Server

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to build a reverse proxy on the Nginx server. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Part 1: installation

1 set up users and groups

/ usr/sbin/groupadd www / usr/sbin/useradd-g www www

2 install pcre so that nginx reverse proxy supports rewrite to facilitate what you need in the future

Wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz tar zxvf pcre-7.8.tar.gz cd pcre-7.8/. / configure make & & make install

3 install nginx reverse proxy

Wget http://sysoev.ru/nginx/nginx-0.7.58.tar.gz tar zxvf nginx-0.7.58.tar.gz cd nginx-0.7.58/. / configure-- user=www-- group=www-- prefix=/usr/ local/webserver/nginx-- with-http_stub_status_module-- with-http_ssl_module-with-cc-opt='-o2'-- with-cpu-opt = opteron make & & make install

Note the above-- with-cc-opt='-o2'-- with-cpu-opt=opteron, which is compiler optimization, and currently the most commonly used is-02 instead of 3. 0. The following corresponds to the model of the cpu.

Part 2: configuration and optimization configuration files

1 nginx.conf profile:

User www www; worker_processes 4; # [debug | info | notice | warn | error | crit] error_log / usr/local/webserver/nginx/logs/nginx_error.log crit; pid / usr/local/webserver/nginx/nginx.pid; # specifies the value for maximum file descriptors that can be opened by this process. Worker_rlimit_nofile 51200; events {use epoll; worker_connections 51200;} http {include mime.types; default_type application/octet-stream; source_charset gb2312; server_names_hash_bucket_size 256; client_header_buffer_size 256k; large_client_header_buffers 4 256k; # size limits client_max_body_size 50m; client_body_buffer_size 256k; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m # parameters have been adjusted. The purpose is to solve some 502 499 errors sendfile on; tcp_nopush on; keepalive_timeout 120; # parameter enlarged to solve 502 errors tcp_nodelay on; include vhosts/upstream.conf; include vhosts/bbs.linuxtone.conf;} when acting as an agent

2 upstream.conf configuration file (this is also the way to configure the load

Upstream.conf upstream bbs.linuxtone.com {server 192.168.1.4 virtual 8099;}

3 site profile

Bbs.linuxtone.conf server {listen 80; server_name bbs.linuxtone.conf; charset gb2312; index index.html index.htm; root/ date/wwwroot/linuxtone/; location ~ ^ / nginxstatus/ {stub_status on; access_log off;} location / {root/ date/wwwroot/linuxtone/; proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header remote-host $remote_addr Proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; client_max_body_size 50m; client_body_buffer_size 256k; proxy_connect_timeout 30; proxy_send_timeout 30; proxy_read_timeout 60; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 Proxy_max_temp_file_size 128m; proxy_pass http://bbs.linuxtone.com;}

The parameters have been adjusted. The purpose is to solve some 502 499 errors in the agent process.

# add expires header for static content location ~ *\. (jpg | jpeg | gif | png | swf) ${if (- f $request_filename) {root/ date/wwwroot/linuxtone/; expires 1d; break;}} log_format access'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent" $http_x_forwarded_for'; access_log / exp/nginxlogs/bbs.linuxtone_access.log access }

Common instruction

Let's take a look at some common instructions for reverse proxy of nginx.

Proxy_pass instruction

Grammar

Proxy_pass [url | upstream]

Action

This directive is used to set the proxy server port or socket, as well as url

Proxy_redirect instruction

Grammar

Proxy_redirect [off | default | redirect replacement]

Action

This directive is used to change the "location" and "refresh" in the reply header header of the proxy server.

Add:

I have not yet mastered the function of this command. The actual settings are all off. If you have any understanding, please leave a message to guide me under the blog.

Proxy_next_upstream instruction

Grammar

The copy code is as follows:

Proxy_next_upstream [error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_404 | off]

Action

This directive is used to set under which circumstances the request is forwarded to the next server. In the upstream load balancing proxy server pool, you can use this directive to forward the request to the next server in the pool, assuming that a server at the back end is inaccessible or returns a specified error response code.

Parameter description

Error: if an error occurs when connecting to the server, sending a request, or reading a reply message

Timeout: timed out when connecting to the server, passing the request, or reading the back-end server reply message

Invalid_header: the back-end server returns an empty or incorrect reply

Http_: the specified reply status code is returned by the backend server.

Off: forbids forwarding of requests to the next backend server

Proxy_set_header instruction

Grammar

Proxy_set_header header value

Action

This directive allows you to redefine or add header lines to the request information forwarded to the proxy server. Its value can be text, a variable, or a combination of text and variables.

These are all the contents of the article "how to build a reverse proxy on a Nginx server". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report