High-performance nginx HTTP server configuration instance

Source: Internet
Author: User

Share the configuration of the nginx http server.

Article 1: HTTP Server

Because tomcat processes static resources slowly, the first thing that comes to mind is to put all static resources (JS, CSS, image, swf)

When talking about individual servers, we use an HTTP server that is faster. nginx is selected here, which is more lightweight than apache,

The 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 use nginx, and Sina, Netease, and QQ all use nginx, which shows that the stability and performance of nginx are still very good.

1. nginx installation (linux)

Http://nginx.org/en/download.html Download latest stable version

Download the corresponding template based on the features you need. The following modules are downloaded:
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 handles static resource configuration

# Start GZIP to compress CSS and JS
Gzip on;
# Compression level 1-9. The default value is 1. The higher the level, the higher the compression rate. Of course, the longer the compression time.
Gzip_comp_level 4;
# Compression type
Gzip_types text/css application/x-javascript;

# Define the service for static resource access, corresponding domain name: res.abc.com
Server {
Listen 80;
Server_name res.abc.com;

# Enable the server to read the file cache,
Open_file_cache max = 200 inactive = 2 h;
Open_file_cache_valid 3 h;
Open_file_cache_errors off;

Charset UTF-8;

# Determine if the image or swf is used, and the client caches the image 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;
}

# Due to JS, CSS changes are frequent, and the client caches the file for 8 hours.
Location ~ * ^. +. (Js | css) $ {
Root/usr/local/resource /;
Access_log off;
Index index.html index.htm;
Expires 8 h;
}

# Other static Resources
Location /{
Root/usr/local/resource;
Access_log off;
Expires 8 h;
}
}

3. nginx reverse proxy settings

# Reverse proxy service, binding domain name www.jbxue.com
Server {
Listen 80;
Server_name www.jbxue.com;

Charset UTF-8;

# BBS use Discuz!
# In order to improve the performance of reverse proxy, some http header information will not be forwarded to the backend server,
# Use proxy_pass_header and proxy_set_header to forward the required http header information to the backend server
Location ^ ~ /Bbs /{
Root html;
Access_log off;
Index. php;
# Forward the host information. If no host is set, the domain name obtained using request. getServerName () in the background is not www.abc.com, but 127.0.0.1.
Proxy_set_header Host $ host;
# Because Discuz! To ensure security, you need to obtain the User-Agent of the client to determine whether the data in each POST is from the same browser as the first request,
# If User-Agent and Discuz are not forwarded! If you submit data, the error "your request is incorrect and cannot be submitted" will be reported.
Proxy_pass_header User-Agent;
Proxy_pass http: // 127.0.0.1: 8081;
}

# Forward other requests to tomcat
Location /{
Root html;
Access_log off;
Index. jsp;
Proxy_pass http: // 127.0.0.1: 8080;
}

Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
}
}

Nginx detailed configuration reference: http://wiki.nginx.org/

  • Simple Nginx image server configuration example
  • Nginx independent image server setup tutorial
  • Nginx high-speed cache image server configuration tutorial
  • Nginx image cache to local browser cache gzip compression in nginx
  • Nginx image Cache Server Configuration instance
  • Nginx Proxy configuration for image cache instance reference
  • Nginx sets the browser cache time for images or static files such as css/js/jpg/png.
  • Nginx Proxy and image cache Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.