Based on Nginx Web server and set up a virtual host configuration Detailed

With the rapid development of computer and Internet technology, a variety of user-oriented Web site has become the backbone. In a variety of web server software. In addition to Apache, as well as a lightweight HTTP server software - Nginx .

Based on the Apache Web server can be set up reference Bowen: Detailed structures based Apache Web server

A, Nginx Intro

Nginx developed by lgor Sysoev of Russia, developed specifically for performance optimization, the most well-known advantages is its stability and low system resource consumption, as well as the ability to stand high concurrent HTTP connections (single physical server can support 30,000 ~ 50000 concurrent requests). Because of this, providing a large number of social networking, news, e-commerce and web hosting, and service companies have chosen to provide Nginx Web services.

If it is to build a Web service in order to resolve static pages, dynamic web pages, do not need too many features, then Nginx is definitely preferred.

Second, compile and install Nginx Service

Nginx latest stable version is 1.12.0, can http://nginx.org/ through the official website or network disk link: https://pan.baidu.com/s/1H5DHcVWMPGDWFQ-kDsS7XA
extraction code: 1zyi
download to use.

1. compile and install Nginx Service

1) installation support software

Nginx configuration and operational needs pcre (support for regular expressions), zlib (supports compressed) and other support packages, so you should install the software development kit to provide to provide the appropriate functionality to ensure the successful completion of the installation of Nginx:

[root@localhost ~]# yum -y install pcre-devel zlib-devel

2) create a running user group

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

3) compile and install Nginx

[root@localhost ~]# tar zxf nginx-1.12.0.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx \
 --user=nginx --group=nginx --with-http_stub_status_module
//指定Nginx服务的安装目录、运行用户及组
启用http_stub_status_module模块支持状态统计,便于查看服务器的连接信息
[root@localhost nginx-1.12.0]# make && make install

4) optimization path

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

Operational control 2.Nginx services

1) Check the service configuration file Nginx

Apache httpd is similar to the main program, Nginx also use the "-t" option to check the syntax of its configuration file. To check the configuration file located in a different location, use the "-c" option to specify the path.

[root@localhost nginx-1.12.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

or

[root@localhost nginx-1.12.0]# nginx -c /usr/local/nginx/conf/nginx.conf

2) start, stop Nginx service

[root@localhost ~]# nginx
//直接运行nginx命令即可启动Nginx服务
[root@localhost ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      46231nginx: master 
//默认也是TCP协议80端口,如果有其他Web服务软件,应修改其端口,避免冲突
[root@localhost ~]# lynx http://127.0.0.1
//可以使用lynx命令进行文本浏览器进行检查(需要安装lynx软件包)
[root@localhost ~]# killall -s HUP nginx
//重新加载Nginx配置文件
[root@localhost ~]# killall -s QUIT nginx
//停止Nginx服务

3) Add Nginx serve as a system service

In order to start Nginx services, stop, reload and other operations more convenient, you can write a script Nginx service. Script reads as follows:

[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
PROG="/usr/local/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
        $PROG
;;
stop)
        kill -s QUIT $(cat $PIDF)
;;
restart)
        $0 stop
        $0 start
;;
reload)
        kill -s HUP $(cat $PIDF)
;;
*)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
        exit 0

[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# systemctl start nginx
//即可使用systemctl工具来管理Nginx服务

3.Nginx service profile Comments

The main configuration file Nginx services as: /usr/local/nginx/conf/nginx.conf.

1) Global Configuration

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user  nginx;                                               //运行用户
worker_processes  1;                                 //工作进程数量

#error_log  logs/error.log;                           //错误日志文件的位置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;                             //PID文件的存放位置

worker_processes configuration item represents the number of work processes, if the server has more than one CPU or multi-core processor, you can refer to the total number of CPU cores to specify the number of worker processes (you can use the command CAT / proc / cpuinfo | grep "Processor" | WC the -l
) ; if the site was visited by the needs of small, usually set to 1 (self-adjusting according to the situation).

2) I / O Event Configuration

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
                    …………………………                 //省略部分内容
events {
    use epoll;                                                     //使用epoll模型
    worker_connections  1024;                         //每个进程处理1024个连接
}

For kernel version 2.6 and above, we recommend using epoll model to improve performance.

3) HTTP Configuration

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
                    …………………………                 //省略部分内容
http {
    include       mime.types;                               //支持多媒体格式
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
     //日志主格式
    access_log  logs/access.log  main;           //访问日志存放位置

    sendfile        on;                                        //开启高效传输文件模式
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;                             //连接超时时间(默认是秒)

    #gzip  on;

    server {                                                    //Web服务的监听配置
        listen       80;                                       //监听地址及端口
        server_name  localhost;                     //网站名称
        charset utf-8;                                      //网页的默认字符集
        location / {                                          //根目录配置(必须存在)
            root   html;                                     //网站根目录的位置
            index  index.html index.php;         //默认首页
        }
        error_page   500 502 503 504  /50x.html;              //内部错误的反馈页面
        location = /50x.html {                                              //错误页面配置
            root   html;
        }

    }
}

statement is used to set the root page document specific access path location, the default is html subdirectory under Nginx installation directory. Self-modified according to the actual situation.

Third, access to state statistics and web hosting application

Access to state statistics 1.Nginx

Nginx built HTTP_STUB_STATUS state statistics module, used to feed the current Web access, the need to add --with-http_stub_status_module compile Nginx to start installing the module. In addition, also times change the configuration file:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
                         ……………………                   //省略部分内容
server {
…………           //省略部分内容,添写以下内容
 location /status{
        stub_status on;
        access_log  off;
        }
}
[root@localhost ~]# systemctl restart nginx

User Test Access:
Based on Nginx Web server and set up a virtual host configuration Detailed
where "Active connections" indicates that the current number of active connections;
"Server accepts the Handled Requests" indicates that the connection information has been processed:
three numbers represent the number of connections in turn processed, the number of requests TCP handshake successful, processed number.

2. The name-based virtual Web host

Web-based virtual host domain name through a domain name to distinguish between different Web sites. When using Nginx build virtual host server, each virtual Web site has a separate "server {}" section to configure their IP address, port number, domain name itself can be specified. This example creates virtual hosts for different domains.

The basic steps to create virtual hosts:

(1) set up DNS service, the two domain names resolve to the same IP address.

DNS service can be set up reference Bowen: Linux set up DNS service

(2) prepare and test files web directory

[root@localhost ~]# mkdir -p /var/www/benet
[root@localhost ~]# mkdir -p /var/www/accp
[root@localhost ~]# echo "www.benet.com" > /var/www/benet/index.html
[root@localhost ~]# echo "www.accp.com" > /var/www/accp/index.html

(3) adjusting the main configuration file Nginx services

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
              ………………                    //省略部分内容
http {
              ………………                    //省略部分内容
  server {
        listen       80;
        server_name  www.benet.com;
        charset utf-8;
        location / {
            root   /var/www/benet;
            index  index.html index.php;
        }
        location /status{
        stub_status on;
        access_log  off;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
    server {
        listen       80;
        server_name  www.accp.com;
        charset utf-8;
        location / {
            root   /var/www/accp;
            index  index.html index.php;
        }
        location /status{
        stub_status on;
        access_log  off;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}
[root@localhost ~]# systemctl restart nginx

(4) Access Web Hosting

Based on Nginx Web server and set up a virtual host configuration Detailed
Based on Nginx Web server and set up a virtual host configuration Detailed

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14157628/2434832