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 configure reverse proxy for nginx

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to configure reverse proxy in nginx". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to configure reverse proxy in nginx.

Preparatory work

System environment: centos 6.5 / 64

Close SELinux

Temporary shutdown: setenforce 0

Permanent shutdown: permanently shutting down selinux requires a system restart, so you can temporarily shut down and then modify the configuration file.

[root@aliyun home] vim / etc/selinux/config#SELINUX=enforcing # comment out # SELINUXTYPE=targeted # comment out SELINUX=disabled # add: wq # Save, close. Release firewall port 80 [root@aliyun cnyunwei] / sbin/iptables-I INPUT-p tcp-- dport 80-j ACCEPT [root@aliyun cnyunwei] / etc/init.d/iptables save # Save firewall to make configuration effective install compiler tool [root@aliyun cnyunwei] yum-y install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl download software [root@aliyun cnyunwei] cd / home/pro # enter the directory to download nginx [ Root@aliyun cnyunwei] wget http://nginx.org/download/nginx-1.9.10.tar.gz download pcre (supports nginx pseudo-static) [root@aliyun cnyunwei] wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz download ngx_cache_purge (clear specified URL cache) [root@aliyun cnyunwei] wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

Extract the related software packages:

[root@aliyun cnyunwei] cd / home/pro/ [root@aliyun pro] tar zxvf pcre-8.37.tar.gz [root@aliyun pro] tar zxvf nginx-1.9.10.tar.gz [root@aliyun pro] tar zxvf ngx_cache_purge-2.3.tar.gz

Install pcre:

[root@aliyun pro] cd pcre-8.37 [root@aliyun pcre-8.37] mkdir / usr/local/pcre [root@aliyun pcre-8.37]. / configure-- prefix=/usr/local/pcre # configure installation path [root@aliyun pcre-8.37] make & & make install

Install nginx:

[root@aliyun pcre-8.37] groupadd nginx & & useradd-r nginx- g nginx- s / sbin/nologin # create users and user groups [root@aliyun pcre-8.37] cd / home/pro/nginx-1.9.10 [root@aliyun nginx-1.9.10] mkdir / usr/local/nginx [root@aliyun nginx-1.9.10]. / configure-prefix=/usr/local/nginx-user=nginx-group=nginx-with-http_stub_status_ Module-with-openssl=/usr/-with-pcre=/home/pro/pcre-8.37-add-module=/home/pro/ngx_cache_purge-2.3 [root@aliyun nginx-1.9.10] make & & make install

Note:-with-pcre=/home/pro/pcre-8.37 and-add-module=/home/pro/ngx_cache_purge-2.3 point to the path where the source package is decompressed, not the path of installation, otherwise an error will be reported.

Gx_cache_purge is compiled and installed with nginx and does not need to be installed separately.

Copy the system service file nginx to / etc/init.d/ after the installation is complete.

[root@aliyun nginx-1.9.10] cd / etc/init.d/ [root@aliyun init.d] chmod 755 nginx chown root.root nginx # set permissions [root@aliyun init.d] chkconfig-add nginx & & chkconfig nginx on # add to system service and set self-startup

The configuration is loaded in the corresponding file and is not written to the main configuration file:

[root@aliyun init.d] cd / usr/local/nginx/conf/ [root@aliyun conf] vim nginx.conf

The profile information is as follows:

# the number of user nginx nginx;#nginx processes belonging to users and user groups, which is recommended to be set to equal to the total number of CPU cores worker_processes 1 * erroraccounlog / home/nginx/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {use epoll;# maximum number of connections per process (maximum number of connections * processes) worker_connections 65535;} http {# load cache include proxy.conf # load proxy server information (can be defined as load balancer polling) include mysvrhost.conf;# load mime type include mime.types;# default file type 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 logs/access.log main # hash table size of the server name server_names_hash_bucket_size 128X # upload file size limit client_header_buffer_size 32k * set request cache large_client_header_buffers 4 32k * * keepalive_timeout * # do not display nginx version information server_tokens off;gzip on;gzip_min_length 1kbot gzipmakers buffers 4 16kash gzipsets httpmakers version 1.1 location gzipmakers compounding level 2th gzipmakers types text/plain application/x-javascript text/css application/xml;gzip_vary on;# forbids nginx empty host heads server {listen 80 default;server_name _; location / {root html;return 404;} location ~ / .gzipsets {deny all;}} # load site configuration information include / home/nginx/vhost/*.conf;}

Create related files and directories:

[root@aliyun conf] mkdir-p / home/nginx/proxy_temp_dir # proxy_temp_dir and proxy_cache_dir must be in the same partition [root@aliyun conf] mkdir-p / home/nginx/proxy_cache_ dirt [root @ aliyun conf] mkdir-p / home/nginx/vhost

Create cache profile: proxy.conf

[root@aliyun conf] cd / usr/local/nginx/conf/ [root@aliyun conf] vim proxy.conf# specifies temporary file directory proxy_temp_path / home/nginx/proxy_temp_dir;# specifies cache path, sets Web cache name to cache_one, memory cache to 500MB, automatically clears files that have not been accessed within 1 day, and hard disk cache is 30GB. Proxy_cache_path / home/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;# defines the maximum number of bytes requested by the buffer proxy buffer client client_body_buffer_size 512kbot # defines the connection backend server timeout proxy_connect_timeout 60th # defines the backend server response request timeout proxy_read_timeout 60X # defines the backend server sending data timeout proxy_send_timeout 60 # define the size of the proxy request cache proxy _ buffer_size 32kt proxy cache buffers 4 64kbot # define the proxy_buffers size that can be applied for when the system is busy, proxy_busy_buffers_size 128kbot # define the size of the proxy cache temporary file proxy_temp_file_write_size 128k # define failover. If the backend server returns errors such as 502,504 or execution timeout, the request is automatically forwarded to another server in the upstream load balancer pool to achieve failover. Proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;# defines the use of web cache cache_oneproxy_cache cache_one

Create a upstream proxied server file:

[root@aliyun conf] vim mysvrhost.confupstream susecuihost {# each request is allocated according to the hash result of accessing the ip, so that each guest accesses a back-end server regularly, which can solve the session problem. For the load balancer of ip_hash;#upstream, weight is the weight, which can be defined according to the machine configuration. The weigth parameter represents the weight, and the higher the weight, the greater the probability of being assigned. # the number of failed requests allowed by max_fails defaults to 1. Returns the error defined by the proxy_next_upstream module when the maximum number of times is exceeded. # fail_timeout is the time to pause after a max_fails failure. Server proxy server IP:80 weight=1 max_fails=2 fail_timeout=30s;#server proxy server IP 2: 80 weight=1 max_fails=2 fail_timeout=30s;}

Create a site information profile:

[root@aliyun conf] cd / home/nginx/vhost/ [root@aliyun vhost] vim cnyunwei.confserver {listen 80 server server name site domain name; upstream server pool proxy_pass http://susecuihost;# configured in location / {# mysvrhost.conf increases the key value to set the web cache, and nginx stores the cache proxy_cache_key $host$uri$is_args$args;proxy_set_header Host $host according to the key value md5 hash # the backend Web server can obtain the user's real IPproxy_set_header X-Forwarded-For $remote_addr;proxy_cache_valid 200 304 12ht exchangres 2dash proxy refuses headers "Cache-Control"Expires"Set-Cookie";} # clear the cache (specify the clear cache parameter) location ~ / purge (/. *) {# allow all IP to clean the cache allow all;#allow 192.168.21.0 The extension files listed in proxy_cache_purge cache_one $host$1 $is_args$args;} # do not cache location ~. *\. (php | jsp | cgi | asp | aspx | flv | swf | xml)? ${proxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr;proxy_pass http://susecuihost;}access_log off;}

You can batch reset user groups for the files you use:

[root@aliyun conf] chown nginx.nginx-R / home/nginx/proxy_cache_dir / home/nginx/proxy_temp_dir / home/nginx/vhost [root@aliyun conf] service nginx start # launch nginx thank you for reading, this is the content of "how to configure reverse proxy for nginx". After the study of this article, I believe you have a deeper understanding of how to configure reverse proxy for nginx. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report