Example 1
Currently, Apache is not comparable in terms of stability. Therefore, it is more appropriate to use nginx for reverse proxy.
Here we will proxy http: // 172.30.170.8: 8088/vod/. The reverse proxy server is named vod.xx.xxx.cn and listens to port 80.
The Apache httpd server listens to port 8088 (apache and the reverse proxy server are on the same server ).
Make some adjustments to global configuration parameters:
The code is as follows: |
Copy code |
Hacklog @ hywd:/etc/nginx $ cat nginx. conf User www-data; Worker_processes 4; Error_log/var/log/nginx/error. log; Pid/var/run/nginx. pid; Events { Use epoll; Worker_connections 2048; # Multi_accept on; } Http { Include/etc/nginx/mime. types; Access_log/var/log/nginx/access. log; Gzip on; Gzip_disable "MSIE [1-6]. (?!. * SV1 )"; Server_names_hash_bucket_size 256; Client_header_buffer_size 256 k; Large_client_header_buffers 4 256 k; # Size limits Client_max_body_size 50 m; Client_body_buffer_size 256 k; Client_header_timeout 3 m; Client_body_timeout 3 m; Send_timeout 3 m; # All parameters are adjusted to solve some 502 errors during the proxy process. Sendfile on; Tcp_nopush on; Keepalive_timeout 120; # increase the parameter to solve the 502 error during proxy. Tcp_nodelay on; Include/etc/nginx/conf. d/*. conf; Include/etc/nginx/sites-enabled /*; } |
The reverse proxy is configured, and some parameters must be adjusted. If client_max_body_size is not adjusted, uploading via web may cause problems:
The code is as follows: |
Copy code |
Hacklog @ hywd:/etc/nginx/sites-available $ pwd /Etc/nginx/sites-available Hacklog @ hywd:/etc/nginx/sites-available $ cat proxy_local_apache # You may add here your # Server { #... #} # Statements for each of your virtual hosts Server { Listen 80 default; Server_name vod.xx.xxx.cn; Access_log/var/log/nginx/vod.xx.xxx.cn. access. log; Location ~ ^/Status /{ Stub_status on; Access_log off; } Location /{ 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 50 m; Client_body_buffer_size 256 k; Proxy_connect_timeout 30; Proxy_send_timeout 30; Proxy_read_timeout 60; Proxy_buffer_size 256 k; Proxy_buffers 4 256 k; Proxy_busy_buffers_size 256 k; Proxy_temp_file_write_size 256 k; Proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; Proxy_max_temp_file_size 128 m; Proxy_pass http: // 172.30.170.8: 8088/vod /; } Location/doc { Root/usr/share; Autoindex on; Allow 127.0.0.1; Deny all; } # Error_page 404/404 .html; Hacklog @ hywd:/etc/nginx/sites-available $ |
Nginx log cutting script
The code is as follows: |
Copy code |
Root @ hywd:/etc # cat/usr/local/sbin/cut_nginx_log.sh #! /Bin/bash # This script run at 00:00 # The Nginx logs path Logs_path = "/var/log/nginx /" Mkdir-p $ {logs_path} $ (date-d "yesterday" + "% Y")/$ (date-d "yesterday" + "% m ")/ Mv $ {logs_path} vod.xx.xxx.cn. access. log $ {logs_path} $ (date-d "yesterday" + "% Y")/$ (date-d "yesterday" + "% m")/vod.xx.xxx.cn. access _ $ (date-d "yesterday" + "% Y % m % d "). log Kill-USR1 'cat/var/run/nginx. Pi' Crontab-e # M h dom mon dow command 00 00 ***/bin/bash/usr/local/sbin/cut_nginx_log.sh |
Example 2
Nginx is a high-performance web server and a powerful reverse proxy server. We only need to download the source code compilation and installation configuration.
1. Install Nginx
1. Install the required PCRE library:
The code is as follows: |
Copy code |
Cd/tmp Ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.gz wget-c Tar-zxvf pcre-8.13.tar.gz Cd pcre-8.13 ./Configure -- prefix =/usr Make Make install |
2. Download nginx source code for installation:
The code is as follows: |
Copy code |
Cd/tmp Http://nginx.org/download/nginx-1.0.8.tar.gz wget-c Tar-zxvf nginx-1.0.8.tar.gz Cd nginx-1.0.8 ./Configure -- user = nginx -- group = nginx -- prefix =/usr/local/nginx -- with-http_addition_module -- with-http_perl_module -- with-http_flv_module -- with-http_gzip_static_module -- with-http_realip_module -- with-http_ssl_module -- with-http_stub_status_module -- with-http_sub_module -- with-http_dav_module -- Make Make install |
3. Create an nginx User:
The code is as follows: |
Copy code |
Useradd nginx-s/sbin/nologin-M |
4. The following are the installation paths displayed after installation:
The code is as follows: |
Copy code |
Nginx path prefix: "/usr/local/nginx" Nginx binary file: "/usr/local/nginx/sbin/nginx" Nginx configuration prefix: "/usr/local/nginx/conf" Nginx configuration file: "/usr/local/nginx/conf/nginx. conf" Nginx pid file: "/usr/local/nginx/logs/nginx. pid" Nginx error log file: "/usr/local/nginx/logs/error. log" Nginx http access log file: "/usr/local/nginx/logs/access. log" Nginx http client request body temporary files: "client_body_temp" Nginx http proxy temporary files: "proxy_temp" Nginx http fastcgi temporary files: "fastcgi_temp" Nginx http uwsgi temporary files: "uwsgi_temp" Nginx http scgi temporary files: "scgi_temp" |
II. Configure nginx
1. Edit the configuration file/usr/local/nginx/conf/nginx. conf.
The code is as follows: |
Copy code |
Vim/usr/local/nginx/conf/nginx. conf |
2. Modify the following content:
Remove the # before the first user and change it:
The code is as follows: |
Copy code |
User nginx; |
Change worker_processes to a greater value, for example, set it to 2:
The code is as follows: |
Copy code |
Worker_processes 2; |
Remove the # before the following two rows:
The code is as follows: |
Copy code |
Error_log logs/error. log Pid logs/nginx. pid |
Bind the domain name or IP address used for replacement:
The code is as follows: |
Copy code |
Listen 80;
|
The domain name or IP address bound to server_name;
3. Configure the reverse proxy content and replace the URL with HttpSubModule:
Find
The code is as follows: |
Copy code |
Location /{ Root html; Index index.html index.htm; |
Add:
The code is as follows: |
Copy code |
Sub_filter: Domain name or IP address to be replaced by the reverse proxy URL; # Replace the URL Sub_filter_once off; # search to replace all rows Proxy_pass http: // The URL for reverse proxy; Proxy_set_header X-Real-IP $ remote_addr; Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for; Proxy_set_header Accept-Encoding "; # clear the Encoding |
3. Compile the nginx startup script and set auto-start upon startup:
The code is as follows: |
Copy code |
Vim/etc/init. d/nginx |
Start script
The code is as follows: |
Copy code |
#! /Bin/sh # # Nginx-this script starts and stops the nginx daemin # # Chkconfig:-85 15 # Description: Nginx is an HTTP (S) server, HTTP (S) reverse # Proxy and IMAP/POP3 proxy server # Processname: nginx # Config:/usr/local/nginx/conf/nginx. conf # Pidfile:/usr/local/nginx/logs/nginx. pid # Source function library. ./Etc/rc. d/init. d/functions # Source networking configuration. ./Etc/sysconfig/network # Check that networking is up. ["$ NETWORKING" = "no"] & exit 0 Nginx = "/usr/local/nginx/sbin/nginx" Prog = $ (basename $ nginx) NGINX_CONF_FILE = "/usr/local/nginx/conf/nginx. conf" Lockfile =/var/lock/subsys/nginx Start (){ [-X $ nginx] | exit 5 [-F $ NGINX_CONF_FILE] | exit 6 Echo-n $ "Starting $ prog :" Daemon $ nginx-c $ NGINX_CONF_FILE Retval =$? Echo [$ Retval-eq 0] & touch $ lockfile Return $ retval } Stop (){ Echo-n $ "Stopping $ prog :" Killproc $ prog-QUIT Retval =$? Echo [$ Retval-eq 0] & rm-f $ lockfile Return $ retval } Restart (){ Configtest | return $? Stop Start } Reload (){ Configtest | return $? Echo-n $ "Reloading $ prog :" Killproc $ nginx-HUP RETVAL =$? Echo } Force_reload (){ Restart } Configtest (){ $ Nginx-t-c $ NGINX_CONF_FILE } Rh_status (){ Status $ prog } Rh_status_q (){ Rh_status>/dev/null 2> & 1 } Case "$1" in Start) Rh_status_q & exit 0 $1 ;; Stop) Rh_status_q | exit 0 $1 ;; Restart | configtest) $1 ;; Reload) Rh_status_q | exit 7 $1 ;; Force-reload) Force_reload ;; Status) Rh_status ;; Condrestart | try-restart) Rh_status_q | exit 0 ;; *) Echo $ "Usage: $0 {start | stop | status | restart | condrestart | try-restart | reload | force-reload | configtest }" Exit 2 Esac |
Set permissions
The code is as follows: |
Copy code |
Chmod 755/etc/init. d/nginx Chkconfig -- level 345 nginx on |