Home>

In fact, I prefer nginx to run static and load reverse proxy.Dynamic PHP is still handed to Apache for stable processing.Jsp is handed over tomcat, resin or jboss. nginx's ability to run static is unparalleled,Is currently the strongest web server.The dynamic and static configuration of nginx and apache, tomcat, and resin is actually very simple.Just a few words of configuration,The stability is also very good.

1. Dynamic and static separation configuration of nginx and apache:

Put the following configuration in the server {} corresponding to the nginx configuration file. If you use other port numbers,Just change it:

#All php's dynamic pages are handled by apache

location ~ \. (php)?${
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:88;
}

#All static files are read directly by nginx without going through apache

location ~. * \. (htm | html | gif | jpg | jpeg | png | bmp | swf | ioc | rar | zip | txt | flv | mid | doc | ppt | pdf | xls | mp3 | wma) $
{expires 15d;}
location ~. * \. (js | css)?$
{expires 1h;}

If fastcgi was set before, comment out the following configuration:

#pass the php scripts to fastcgi server listening on 127.0.0.1:9000
#
#location ~ \ .php ${
#root/var/www/html;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#include fastcgi.conf;
#}

Restart nginx to take effect, as shown in the figure,The header shows nginx and phpinfo shows apache, indicating that the dynamic and static separation takes effect.

Dynamic and static separation configuration of 2.niginx and tomcat:#Main profile configuration

[[email protected] ~] #vi /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
  worker_connections 1024;
}
http {
  include mime.types;
  default_type application/octet-stream;
  #LOG Format Definition
  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;
  sendfile on;
  keepalive_timeout 65;
  #gzipCompression feature settings
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascripttext/css application/xml;
  gzip_vary on;
  server {
    listen 80;
    server_name www.test.com;
    location/{
  #jspWebsite root directory,Generally nginx and tomcat are in the same directory
      root/usr/local/tomcat/webapps/root;
      index index.html index.jsp index.html;
    }
    location ~. *. jsp ${
    index index.jsp;
    proxy_pass http://127.0.0.1:8080;#Request from jsp to tomcat for processing
    proxy_redirect off;
    proxy_set_header host $host;#The back-end web server can obtain the user's real IP through x-forwarded-for
    proxy_set_header x-real-ip $remote_addr;
    proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
    client_max_body_size 10m;#Maximum number of single file bytes allowed by the client
    client_body_buffer_size 128k;#The maximum number of bytes that the buffer proxy buffers for the client request
    proxy_connect_timeout 90;#nginx connection timeout time with backend server (proxy connection timeout)
    proxy_read_timeout 90;#After successful connection,Backend server response time (proxy receive timeout)
    proxy_buffer_size 4k;#Set the buffer size of the proxy server (nginx) to save user header information
    proxy_buffers 6 32k;#proxy_buffers buffer, if the average web page is below 32k,Set like this
    proxy_busy_buffers_size 64k;#Buffer size under high load (proxy_buffers * 2)
    proxy_temp_file_write_size 64k;#Set the cache folder size,Greater than this value,Will be passed from the upstream server
    }
    location ~. * \. (gif | jpg | png | bmp | swf) $#Static pages are handled by nginx
    {
    expires 30d;#Use expires cache module,Cache to client for 30 days
    }
    location ~. * \. (jsp | js | css)?$
    {
    expires 1d;
    }
    error_page 404 /404.html;#Error page
    error_page 500 502 503 504 /50x.html;
    location=/50x.html {
      root html;
    }
  }

#Write nginx start, stop, restart and other sysv management scripts,easy to use

[[email protected] ~] #vi /etc/init.d/nginx
#!/bin/bash
#chkconfig:345 99 20
#description:nginx servicecontrol script
prog="/usr/local/nginx/sbin/nginx"
pidf="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$prog
echo "nginx servicestart success."
;;
stop)
kill -s quit $(cat $pidf)
echo "nginx service stopsuccess."
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s hup $(cat $pidf)
echo "reload nginx configsuccess."
;;
*)
echo "usage:$0 {start | stop | restart | reload}"
exit 1
esac
[[email protected] ~] #chmod + x /etc/init.d/nginx
[[email protected] ~] #service nginx restart
[[email protected] ~] #chkconfig --add nginx
[[email protected] ~] #chkconfig nginx on
  • Previous Those things between Star Wars and Python
  • Next Set as homepage and add favorites to each browser based on JavaScript code