High performance WEB development nginx HTTP server article

  • 2020-05-06 12:11:55
  • OfStack

1: HTTP server

Because tomcat processing static resources speed is slow, so first think of is all static resources (JS CSS, image, swf)

For separate servers, use the faster HTTP server, nginx is selected here, nginx is more lightweight than apache,

Configuration is simpler, and nginx is not only a high-performance HTTP server, but also a high-performance reverse proxy server.

At present, many large websites have used nginx, sina, netease, QQ and so on have used nginx, indicating that the stability and performance of nginx is very good.

1. nginx installation (linux)

http: / / nginx org/en/download html
download the latest stable version
According to their needs to download the corresponding template, here downloaded the following modules:
openssl-0.9.8l, zlib-1.2.3, pcre-8.00

Compile and install nginx:
./configure
--without-http_rewrite_module
--with-http_ssl_module
--with-openssl=../../lib/openssl-0.9.8l
--with-zlib=../../lib/zlib-1.2.3
--with-pcre=../../lib/pcre-8.00
--prefix=/usr/local/nginx

make

make install

2, nginx handle the configuration of static resources

Start GZIP and compress CSS and JS
gzip on;
The default is 1. The higher the compression rate is, the longer the compression time will be gzip_comp_level 4;
# compression type
gzip_types text/css application/x-javascript;

Define the service for static resource access with the domain name res.abc.com
server {
listen 80;
server_name res.abc.com;

Turn on the server to read the file cache,
open_file_cache max=200 inactive=2h;
open_file_cache_valid 3h;
open_file_cache_errors off;

charset utf-8;

If it is a picture or swf, the client cache
for 5 days location ~* ^.+.(ico|gif|bmp|jpg|jpeg|png|swf)$ {
root /usr/local/resource/;
access_log off;
index index.html index.htm;
expires 5d;
}

Because JS and CSS were changed frequently, the client side cached
for 8 hours location ~* ^.+.(js|css)$ {
root /usr/local/resource/;
access_log off;
index index.html index.htm;
expires 8h;
}

Other static resources
location / {
root /usr/local/resource;
access_log off;
expires 8h;
}
}

3, nginx reverse proxy set

# reverse proxy service to bind the domain name www.abc.com
server {
listen 80;
server_name www.abc.com;

charset utf-8;

# BBS use Discuz!
In order to improve the performance of the reverse proxy, part of the http header information will not be forwarded to the server in the background,
# forward the required http header information to the backend server
using proxy_pass_header and proxy_set_header location ^~ /bbs/ {
root html;
access_log off;
index index.php;
If host is not set, the domain name obtained by using request.getServerName () in the background is not www.abc.com, but 127.0.0.1
proxy_set_header Host $host;
# for Discuz! For security, you need to get the client User-Agent to determine whether each POST data comes from the same browser as the first request,
If you don't forward User-Agent,Discuz! When you submit the data, you will report an error
that says, "your request was incorrectly routed and cannot be submitted. proxy_pass_header User-Agent;
proxy_pass http://127.0.0.1:8081;
}

Other requests are forwarded to tomcat
location / {
root html;
access_log off;
index index.jsp;
proxy_pass http://127.0.0.1:8080;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

nginx detailed configuration reference: http: / / wiki nginx. org/
Reprint please indicate the source: http: / / www blogjava. net/BearRui/


Related articles: