The basic configuration of Nginx Server learning Nginx

Source: Internet
Author: User
Tags nginx server

This article uses the Linux CentOS system

First, the configuration of Nginx virtual host

Virtual Hosting : Typically, to make each server available to more users, you can divide a server into virtual sub-servers, each of which is independent of each other. These servers are based on virtualization technology, so that a single server can be virtualized into many sub-servers. We call the server a virtual host. After we set up the Nginx server, there is only one Nginx server at this time, if we do the virtual host configuration of this server, we can split an Nginx server into multiple independent sub-servers.

There are two main steps to configure a virtual host in Nginx:

1. Create a virtual host IP to view its host IP through ifconfig and then create a virtual host IP based on the host IP. Command: Ifconfig eth1:2 121.42.41.145 broadcast 121.42.43.255 netmask 255.255.252.0 after execution

B, the second step is to bind the IP address with the virtual host. Nginx.conf: This file is nginx system configuration file, it is recommended not to change this. We typically use a custom file and then load the file to achieve the same effect.

Create the configuration file in the/usr/local/nginx/conf directory under xnzj.conf.

#======== The number of work-derived processes (recommended to be set to the same or twice times the number of CPU cores) ==========Worker_processes 1;#=========== Set the maximum number of connections ==============Events {Worker_connections1024;}#information about the ============http protocol ==============http {server {#=========== to listen for the IP address and port of the virtual host ==========Listen 121.42.41.144:80; #=========== The name of the virtual host ===========server_name 121.42.41.144; #=============== The log file for this virtual host server =========Access_log logs/Server144.access.log combined; #============== Default Request resource =============Location/{root HTML/server144; #===== Nginx will first find index.html if not found, find index.htmindex index.html index.htm; }} server {#=========== to listen for the IP address and port of the virtual host ==========Listen 121.42.41.145:80; #=========== The name of the virtual host ===========server_name 121.42.41.145; #=============== The log file for this virtual host server =========Access_log logs/Server145.access.log combined; #============== Default Request resource =============Location/{root HTML/server145;        Index index.html index.htm; }    }}

Create the corresponding virtual host default resource/usr/local/nginx/html/server144/index.html under/usr/local/nginx/html;/usr/local/nginx/html/server145 /index.html

C, let Nginx load me custom configuration file (My profile: xnzj.conf) Execute command:/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/xnzj.conf

Second, the configuration of the log

Nginx server is running, there will be various operations, these key operations information will be recorded in the file, these files are called log files. Log file records are formatted, we can be in the system by default format to record, or in our custom format to record. We can use the Log_format directive to set the logging format for the Nginx server's log files.

How to configure : Open the nginx.conf file, opening the lower code for the comment.

     #combined: Log Output format     #REMOTE_ADDR Client Request address     #remote_user: Client user name     #Request: Requested address (server resource location)     #Status : The user's request state     #body_bytes_sent: The resource size (in bytes) of the server response,     #http_referer: Source Web page     #http_user_agent: client browser Information     #http_x_forwarded_for: Client IP addressLog_format combined'$remote _addr-$remote _user [$time _local] "$request"'                      '$status $body _bytes_sent "$http _referer"'                     '"$http _user_agent" "$http _x_forwarded_for "'; #================== log file access_log:off; close log ===========Access_log Logs/access.log combined;

Log cutting:
In order to make the Nginx log file store more reasonable and orderly, we need to store the log files separately, for example, we can separate by time, today's log files are stored in a file, tomorrow's log files are stored in another new file, and so on. This time, we will use the log file cutting operations.

Log cutting steps:

1. Create a batch file

Execute in/usr/local/nginx/logs directory [[email protected] logs]# Touch cutlog.sh

2. Add content to the file:

d=$ (date +%y%m%/usr/local/nginx/logs/-USR1 $ (cat/usr/local/nginx/nginx.pid)

3. Execute the batch file Execution crontab-e command to add the following content regularly

* * * */bin/bash/usr/local/nginx/logs/cutlog.sh

Three, Nginx cache configuration

When we browse a page in a browser, we store some of the information on the page (such as a picture on this page) locally, and when we browse the page for the second time, some of the information on the page can be loaded locally, which is much faster. This information stored locally is referred to as caching. However, when the cache is too high, the cache file will be very large, affecting our normal internet activities. Therefore, the cache needs to be cleaned regularly.

configuration:Add the following code under location in the http{server{}} of the/usr/local/nginx/conf/nginx.conf configuration file:

# ==================== Cache configuration =============       Location ~.*\. (jpg|png|swf| gif) ${            expires 2d; # two days to clear        }       ~.*\. (css| js)? ${        expires:1h; #       }
Four, Nginx gzip compression configuration

The compression function we mentioned here refers to the gzip compression technique. Gzip compression technology, you can make the original page content size compressed into the original 30%, so that when users visit the Web page, because the content of the transmission is much smaller than the original content, so access will be much faster. The Nginx server supports gzip compression technology, however, it needs to be configured.

Configuration: Add the following code to the http{} of the/usr/local/nginx/conf/nginx.conf configuration file:

    gzip on  ;  # Open compression    gzip_min_lenth 1k;  # Set the minimum unit of compression using    gzip_buffers 4 16k;  # Create compressed file cache size    gzip_http_version 1.1;  # protocols using compression technology and their version    Gzip_vary:on;  # open to determine whether the client browser supports compression technology
Five, nginx automatic column directory configuration   

When a client accesses a folder on a server through a browser, if the folder has a default home page file, such as index.html, the user will automatically be given access to the Index.html page. However, when there is no index.html the default home page of the file, it is assumed that there are other files at this time, without the ability to configure automatic column directory, the user cannot access the contents of our folder. But after we have configured the Automatic column directory feature, we can see a list of all the files under that folder, which are automatically listed.

There are two conditions required to implement an automatic column catalog:

1. The default home file, such as index, does not exist under the folder accessed.

2. The server is configured with the Automatic column directory feature.

Configuration: Add the following code to the Http{server{}} of the/usr/local/nginx/conf/nginx.conf configuration file:

Location/ {            root   html;            Index  index.html index.htm;            AutoIndex on; # open Automatic column directory        }

The basic configuration of Nginx Server learning Nginx

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.