The file directory of the first real nginx server configuration

Source: Internet
Author: User
Tags nginx server nginx load balancing

Download Nginx server: Download the Nginx version required in http://nginx.org.

Unzip the downloaded nginx into the directory. His directory structure is as


NGINX directory Structure :

1.nginx.exe: (★ ¡ï Important) This is the boot nginx server file. Nginx consumes 80 ports by default. Note: You double-click the icon, the Nginx server will start, and then the black box will disappear, which is normal.

2.logs: This is the Nginx log file. (★ ¡ï Important). Used to record Nginx case, if the nginx error. You can see the contents of this file. So as to solve the nginx problem. After entering the logs directory there will be two important files:

Where Access.log: This file records all records that access the Nginx server.

Where Error.log: This file records all error messages that access the Nginx server. If an error occurs. Can be viewed in this file. For example:

2016/04/18 15:04:37 [ERROR] 9128#8368: CreateFile () "D:\nginx/html/ss" failed (2:the system cannot find the file Speci fied), client:127.0.0.1, Server:localhost, Request: "Get/ss http/1.1", Host: "LocalHost"

Just prompt you:"D:\nginx/html/ss" failed did not find the SS file. That is, the SS file is missing. To solve the problem.

To develop the habit of reading the diary

3.html files: By default, Nginx will view from this to see if there is any need for the file content.

4.CONF (★ ¡ï Important) This is the Nginx configuration file. Inside is the Nginx server configuration information.

One of the important configuration files is: nginx.conf (★ ¡ï Important) Basically nginx all the configuration is done in this file.

nginx.conf Main information:

Nginx has a lot of blocks, the outermost layer is Main,main contains events and HTTP, while HTTP contains upstream and multiple servers, and the server contains multiple localhost.

Where main: is the global setting

Server: Host settings, primarily for specifying domain names and port numbers

Upstream: Load balancer server settings, set up a series of back-end servers

Localhost:url matches a specific location setting, matching the path of a Web page

1.worker_processes: The number of processes that represent work. Generally write the number of cores for your cup

2.http: Set up an HTTP server and use its reverse proxy feature to provide load balancing support. There are some sub-properties below http: for example

2.1 Include: Sets the type of MIME. There are mime.types file definitions:

which

The format used to set the logging.

2.2 Keepalive_timeout: Represents the time-out for keepalive.

2.3 CharSet: Used to set the default character encoding

2.4 Upstream: Currently Nginx supports 3 upstream mode configurations

2.4.1 Polling mode, which accesses all servers in turn. One server access at a time. That is, each request is individually assigned to a different server in chronological order.

2.4.2 Weight weight mode. The larger the value the greater the weight.

2.4.3 Ip_hash: An IP address will parse the hash value, that is, an IP address fixed access to a fixed IP address, which can directly solve the session problem.

2.4 Load Balancing Configuration
The following sets the list of servers for load balancing:

1234567
Upstream Cszhi.com{ip_hash;server 192.168.8.11:80;server 192.168.8.12:80  down;server 192.168.8.13:8009  Max _fails=3  fail_timeout=20s;server 192.168.8.146:8080;}

Upstream is the Nginx HTTP upstream module, which uses a simple scheduling algorithm to load balance client IP to back-end servers.
In the above settings, a load balancer name cszhi.com is specified by the upstream directive. This name can be arbitrarily specified, and will be called directly in the following areas.

nginx load Balancing module currently supports 4 scheduling algorithms, described below, two of which are third-party scheduling methods.

    • polling (default): Each request is assigned to a different back-end server in chronological order, and if a server on the backend is down, the fault system is automatically rejected, make user access unaffected;
    • weight: Specifies the polling weights, the higher the Weight value, the higher the access probability allocated, mainly for the backend performance of each server is uneven;
    • ip_hash: Each request is allocated according to the hash result of the access IP, so that visitors from the same IP have fixed access to a back-end server, It effectively solves the problem of session sharing in Dynamic Web pages,
    • fair: A more intelligent load balancing algorithm than the two above. This algorithm can intelligently load balance according to the page size and the load time, that is, the response time of the backend server to allocate the request, the response time is short priority allocation. Nginx itself is not support fair, if you need to use this scheduling algorithm, you must download Nginx upstream_fair module;
    • url_ Hash: According to the hash result of the access URL to allocate the request, so that each URL directed to the same back-end server, can further improve the efficiency of the back-end cache server. Nginx itself is not support url_hash, if you need to use this scheduling algorithm, you must install Nginx hash package.

In the HTTP upstream module, you can specify the IP address and port of the back-end server through the server directives, and you can also set the state of each back-end server in the load-balancing schedule. The commonly used statuses are:

    • Down: Indicates that the current server is temporarily not participating in load balancing;
    • Backup: Reserved Standby machine. When all other non-backup machines fail or are busy, the backup machine is requested, so the pressure of the machine is the lightest;
    • Max_fails: The number of times the request failed to be allowed, by default, 1. Returns the error defined by the Proxy_next_upstream module when the maximum number of times is exceeded;
    • Fail_timeout: The time to pause the service after a max_fails failure. Max_fails can be used with fail_timeout.

Note that when the load scheduling algorithm is Ip_hash, the state of the backend server in load balancing scheduling cannot be weight and backup.

2.5 Server: Used to configure the virtual machine. His sub-attributes are:

2.5.1 Listen: The port number that represents the listener. The default is port 80.

2.5.2 server_name represents the domain name that is accessed. Domain name can have multiple, in the middle with a space to split

2.5.3 Index: The default first address for setting up access

2.5.4root: The root directory of the Web page used to set the host

2.5.5 localhost: Used to set the reverse proxy, url matching configuration. Used to set static pages and reverse proxy mechanisms

The localhost support regular expression also supports conditional-judgment matching.

The file directory of the first real nginx server 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.