Nginx configuration, virtual host, load balancer, and reverse proxy

Source: Internet
Author: User
Tags epoll

Nginx in front, I wrote several, similar introduction rotten it on each platform installation configuration and PHP-FPM connection and so on. Today, this article focuses on its configuration, including the balance of liabilities and reverse proxies it excels at, as well as the virtual hosting capabilities. Nginx Startup and shutdown

According to the Convention, the configuration of each platform is described first:

CentOS platform, source installed:

/usr/local/nginx/nginx # start/usr/local/nginx/nginx-s reload #平滑重启/usr/local/nginx/nginx.conf #配置文件

Mac platform that I installed with brew.

/usr/local/bin/nginx # start/usr/local/bin/nginx-s reload #平滑重启/usr/local/etc/nginx/nginx.cnf #配置文件. nginx.conf configuration file Detailed

In fact, compared to the Apache configuration file, it is relatively clear and simple, before it felt difficult, now sink the heart to think, actually very simple. Under the general sub-block, the basic is divided into the following blocks:

Main Events {....} http {.... upstream MyProject {...}} server {....... {.....}} {.... ... } } .... }

The Nginx configuration file is divided into six main areas:
Main (Global Settings), events (nginx working mode), HTTP (HTTP settings),
Sever (Host settings), location (URL match), Upstream (Load balancer server settings). Main module

Below a main area, he is a global setting:

User nobody nobody; Worker_processes 2; Error_log/usr/local/var/log/nginx/error.log notice; Pid/usr/local/var/run/nginx/nginx.pid; Worker_rlimit_nofile 1024;

User to specify the Nginx worker process running users and user groups, which are run by default by the nobody account.

Worker_processes to specify the number of child processes that Nginx will open. Each nginx process consumes 10m~12m memory on average. Based on experience, it is generally sufficient to specify 1 processes, and if it is a multi-core CPU, it is recommended to specify the same number of processes as the CPU. I write 2 here, then it will open 2 sub-processes, a total of 3 processes.

The error_log is used to define a global error log file. The log output levels are debug, info, notice, warn, error, crit, where debug output logs are the most verbose, and crit output logs are minimal.

The PID is used to specify the location of the file where the process ID is stored.

The worker_rlimit_nofile is used to specify the maximum number of file descriptors a Nginx process can open, which is 65535 and needs to be set using the command "Ulimit-n 65535". Events Module

The events module uses the specified nginx operating mode and operating mode, and the maximum number of connections, which is generally the case:

events {use kqueue; #mac平台 worker_connections 1024;}

Use is used to specify the working mode of Nginx. The working modes supported by Nginx are SELECT, poll, Kqueue, Epoll, Rtsig and/dev/poll. Where select and poll are standard working modes, kqueue and epoll are efficient modes of operation, unlike Epoll used on Linux platforms, and Kqueue used in BSD systems because the MAC is based on BSD, so Mac has to use this mode, For Linux systems, the Epoll mode of operation is preferred.

The worker_connections is used to define the maximum number of connections per session of Nginx, that is, the maximum number of requests received by the front end, which defaults to 1024. The maximum number of client connections is determined by worker_processes and Worker_connections, which is max_clients=worker_processes*worker_connections, when acting as a reverse proxy, Max_ Clients changed to: max_clients = worker_processes * WORKER_CONNECTIONS/4.
The maximum number of connections for a process is limited by the maximum number of open files for the Linux system process, and the settings worker_connections after the operating system command "Ulimit-n 65536" take effect. HTTP Module

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.