Tools and middleware --Nginx, from basic principles to development practices

table of Contents

I. Introduction

Two, Nginx server three functions --HTTP

Three, Nginx three functions - Web Hosting

3.1 Web Hosting

3.2 Nginx configuration file structure

3.3 Nginx three kinds of virtual host mode - ip-based virtual host configuration

3.4 Nginx three kinds of virtual host mode - based virtual host port configuration

3.5 Nginx three kinds of virtual host mode - based virtual hosting configuration

Four, Nginx three functions - Load Balancing (reverse proxy + weight weights)

4.1 Reverse Proxy

4.1.1 Forward Proxy and Reverse Proxy

4.2 Nginx reverse proxy to achieve

4.3 Nignx load balancing

4.4 Nginx three functions together Summary

Fifth, practice --Nginx installation (for example to centos7.0)

5.1 installed Nginx

5.2 Additional related operations 

VI Summary


I. Introduction

What Nginx that?

       Nginx is a lightweight Web server / reverse proxy server and e-mail (IMAP / POP3) proxy server, distributed under BSD-like agreement. It features occupy less memory, high concurrency, concurrent nginx fact the same type of ability in web server performance is better, mainland China use nginx web site users are: Baidu, Jingdong, Sina, Netease, Tencent, Taobao.

Nginx as a high-performance HTTP server and reverse proxy web server, including three main functions: a static server (as a Web server), virtual host (as a mail proxy server), reverse proxy (as server load balancing). In this paper three functions Nginx server main line, divided into four parts, namely:

The second part of the paper describes "Nginx three functions --HTTP server";

The third part introduces the full text of "Nginx three major functions - Web Hosting";

The fourth paper describes "Nginx three major functions - Load Balancing (reverse proxy + weight weights)";

The full text of Section V "practice --Nginx installation."

Nginx difference between the three functions (functions on three Nginx, a lot of interpretation on the blog is not very clear, here is explained, focusing on the difference)

(1) HTTP server (static resource server / image server)

Nginx HTTP server as: is when the web project development, the project will always deployed on tomcat jetty and other application server, and the server resources needed to text and static data path natural resources stored in the database, and static resource entity (such as pictures, audio and video, etc.) can neither be placed in the database, they can not be placed under the relevant directory of the application server, this time we need to store the required server projects a method for static resources (as long as the field is stored in the database path to good static resources), which is the http server --Nginx.

PS: Why is it called image server, because it is the most commonly used static resource server is the picture (although not necessarily stored on Nginx picture).

(2) reverse proxy / load balancing

Nginx reverse proxy: Nginx as a proxy server, represents the actual application server to communicate with the client, will be distributed to the background network request the actual server.

Load balancing Nginx: Nginx as a proxy server, based on weight weights will be distributed to the background network request the actual server.

The relationship between the reverse proxy and load balancing: Load balancing is achieved through a reverse proxy, load balancing, reverse proxy = + weight weights

(3) Web Hosting

Nginx Web Hosting: Nginx deployed on a physical server, but outside the entrance to implement multiple access by ip, port, domain name, so that the client thought it was more than one server, this is the virtual hosts (specifically how to achieve virtual, provide an explanation below ).

What is the difference between virtual hosts and load balancing is?

Nginx to do virtual hosting, multiple servers will be used as a server to a virtual server through the file directory (the host), web hosting refers to himself as a Nginx server storing files;

Nginx to do load balancing, Nginx as a server proxy server, the network distribute requests to a specific application server (tomcat or jetty), depending on the application server (tomcat jetty) performance, set weight weights, Nginx does not act as a server to store files .

Summary: http server refers to nginx own store pictures and other static files nginx web host is to provide multiple entry to store pictures and other static files nginx load balancing based on the weight refers to the weight distributed network requests a specific application server

Two, Nginx server three functions --HTTP

Nginx is an excellent static server, doing Javaweb development, it is often used to store static resources, picture resources generally, so in turn called Nginx server picture, then the picture address into a relational database (usually Mysql) in. About Nginx as configuring a static server, let me see below.

Nginx-- modified configuration nginx installation directory /conf/nginx.conf (here /usr/local/nginx/conf/nginx.conf file), as follows:

​​server {
        listen       81; # 监听的端口
        server_name  localhost; # 域名或ip
        location / {      # 访问路径配置
            root   /usr/local/nginx/index;# 根目录
            index  index.html index.htm; # 默认首页
        }
        error_page   500 502 503 504  /50x.html;      # 错误页面
        location = /50x.html {
            root   /usr/local/nginx/html;
        }
    }

Now we come to the code interpretation, in fact, to do Nginx servers, and other servers as long as they find out "ip: port page back catalog" Three things like:

Specify the ip: port is localhost: 81, we enter localhost in the browser directly linux computer: 81, shows index.html / index.htm interface, the corresponding background nginx installation directory is the directory / index directory (such as usr / local / nginx / index);

We entered directly in the browser localhost linux computers: 81 / 50x.html, corresponding to the background directory is the installation directory nginx / html directory (such as usr / local / nginx / html).

Of the matching relation, the above configuration / represents all match, / 50x.html represents matching 50x.html, precedence relationships preferential matching is long,

As localhost: 81 / 50x.html meet and / /50x.html, since the length of the matching priority, so to guide the html directory.

As localhost: 81 / xxx.html satisfied only / directory index to guide it.

On page priority relationship, index index.html index.htm; precedence relationships from left to right, there are index.html index.html is displayed, not displayed index.htm.

Figure:

Additional: knowledge about the application server and static servers: I said software server - Past and Present (web server + application server) server software

Three, Nginx three functions - Web Hosting

3.1 Web Hosting

What is Web Hosting?

Hosting is a special hardware and software technology, it can be every computer on the network into a plurality of virtual hosts, each virtual host can independently provide external www services, this can be achieved to provide a plurality of external host web service, are independent between each virtual host, affect each other (do not understand, it does not matter, little by little, to see below).

nginx can be achieved by configuring virtual hosts, nginx supports three types of virtual host configuration, namely: 1, ip-based virtual hosts, and 2, based on the virtual host ports, 3, based virtual hosting

3.2 Nginx configuration file structure

......
events {
    .......
}
http{
   .......
   server{
	.......
	}
   server{
	.......
	}

}

The above code, each server is a virtual host. In fact, that is the same machine, use a different directory into different virtual access entrance. Which, due to different ip form of different access entry called "ip-based virtual hosting" and different access inlet port due to different form called "port-based virtual hosting," because the domain name different form of different access entrance says It is "name-based virtual hosts."

3.3 Nginx three kinds of virtual host mode - ip-based virtual host configuration

A nginx server bind two ip: 192.168.101.3,192.168.101.103, different access request a different ip html directory, namely: access http://192.168.101.3 access html pages under "html3" directory, visit http://192.168.101.103 will visit the html page under "html103" directory, as shown:

For the explanation of FIG:
a physical servers, two ip address, respectively, and 192.168.101.3:80 192.168.101.103:80, open to the outside client, if there are two servers, a request can be an independent network, because they are a different background file directory, namely / usr / local / nginx / html3 and / usr / local / nginx / html103 , will not have any data files interference

Nginx-- modified configuration nginx installation directory /conf/nginx.conf (here /usr/local/nginx/conf/nginx.conf file), as follows:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;
    #配置虚拟主机192.168.101.3
    server {
	#监听的ip和端口,配置192.168.101.3:80
        listen       80;
	#虚拟主机名称这里配置ip地址
        server_name  192.168.101.3;
	#所有的请求都以/开始,所有的请求都可以匹配此location
        location / {
	    #使用root指令指定虚拟主机目录即网页存放目录
	    #比如访问http://ip/test.html将找到/usr/local/html3/test.html
	    #比如访问http://ip/item/test.html将找到/usr/local/html3/item/test.html

            root   /usr/local/nginx/html3;
	    #指定欢迎页面,按从左到右顺序查找
            index  index.html index.htm;
        }

    }
    #配置虚拟主机192.168.101.103
    server {
        listen       80;
        server_name  192.168.101.103;

        location / {
            root   /usr/local/nginx/html103;
            index  index.html index.htm;
        }

    }

}

3.4 Nginx three kinds of virtual host mode - based virtual host port configuration

nginx对外提供80和8080两个端口监听服务,请求80端口则请求html80目录下的html,请求8080端口则请求html8080目录下的html,如图:

对于这个图的理解是:
一个物理服务器,提供两个ip地址,分别是192.168.101.3:80和192.168.101.3:8080,在外界客户端开来,好像有两个服务器,都可以独立网络请求,因为它们后台是不同的文件目录,分别是/usr/local/nginx/html80和/usr/local/nginx/html8080,不会产生任何数据文件干扰

配置Nginx——修改nginx安装目录/conf/nginx.conf(这里是/usr/local/nginx/conf/nginx.conf文件),如下:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;
    #配置虚拟主机
    server {
	#监听的ip和端口,配置80
        listen       80;
	#虚拟主机名称这里配置ip地址
        server_name  192.168.101.3;
	#所有的请求都以/开始,所有的请求都可以匹配此location
        location / {
	    #使用root指令指定虚拟主机目录即网页存放目录
	    #比如访问http://ip/test.html将找到/usr/local/html3/test.html
	    #比如访问http://ip/item/test.html将找到/usr/local/html3/item/test.html

            root   /usr/local/nginx/html80;
	    #指定欢迎页面,按从左到右顺序查找
            index  index.html index.htm;
        }

    }
    #配置虚拟主机
    server {
        listen       8080;
        server_name  192.168.101.3;

        location / {
            root   /usr/local/nginx/html8080;
            index  index.html index.htm;
        }

    }

}

3.5 Nginx三种虚拟主机方式——基于域名的虚拟主机配置

两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。两个域名是aaa.test.com和bbb.test.com,nginx服务器使用虚拟机192.168.101.3 ,如图:

对于这个图的理解是:
一个物理服务器,提供两个域名地址,分别是aaa.test.com和bbb.test.com,在外界客户端开来,好像有两个服务器,都可以独立网络请求,因为它们后台是不同的文件目录,分别是/usr/local/aaa_html和/usr/local/bbb_html,不会产生任何数据文件干扰

在192.168.101.3上创建/usr/local/aaa_html,此目录为aaa.test.com域名访问的目录

在192.168.101.3上创建/usr/local/bbb_html,此目录为bbb.test.com域名访问的目录

配置Nginx——修改nginx安装目录/conf/nginx.conf(这里是/usr/local/nginx/conf/nginx.conf文件),如下:


#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

#配置虚拟主机aaa.test.com 
server {
		#监听的ip和端口,配置本机ip和端口
        listen 192.168.101.3:80;		
		#虚拟主机名称是aaa.test.com,请求域名aaa.test.com的url将由此server配置解析
        server_name aaa.test.com;	 
		#所有的请求都以/开始,所有的请求都可以匹配此location
        location / {
		#使用root指令指定虚拟主机目录即网页存放目录
		#比如访问http://ip/test.html将找到/usr/local/aaa_html/test.html
		#比如访问http://ip/item/test.html将找到/usr/local/aaa_html/item/test.html
                root /usr/local/aaa_html;	
			    #指定欢迎页面,按从左到右顺序查找
                index index.html index.htm;	
        }
    }

#配置虚拟主机bbb.test.com
    server {
        listen 192.168.101.3:80;
        server_name bbb.test.com;
        location / {
                root /usr/local/bbb_html;
                index index.html index.htm;
        }
    }
}

在三种虚拟主机(基于IP的虚拟主机、基于端口的虚拟主机、基于域名的虚拟主机)中,项目开发中使用的最多的是基于域名的虚拟主机。

对于基于IP的虚拟主机:由于ip地址本来就稀缺,一个物理服务器配两个ip是不合实际的(毕竟,一个ip比一个服务器贵),所以,基于ip的虚拟主机不常用到,读者了解即可;

对于基于端口的虚拟主机:端口倒是不稀缺0-65535(一共65536个,默认是80端口),但是想让用户在浏览器上输入端口是非常不切实际的,所以,基于ip的虚拟主机也不常用到,读者了解即可;

对于基于域名的虚拟主机:一个物理服务器/服务器群(一个ip)配置多个域名,是可行的,因为:

一来,域名平均价格比ip便宜的多(即租用多个域名比多个ip便宜,所以基于域名的虚拟主机比基于ip的虚拟主机便宜,注意:这里使用了平均两个字,希望读者不要抬杠);

二来,用户可以容易的记得域名,然后在浏览器中输入域名;

所以,基于ip的虚拟主机经常用到,读者一定要知道,下面我们将反向代理和负载均衡,在上面的一层会用到虚拟主机,都是基于域名的虚拟主机。

四、Nginx三大功能——负载均衡(反向代理+weight权重)

4.1 反向代理

要搞清楚什么是负载均衡,我们先要搞清楚什么是反向代理,因为负载均衡是在反向代理的基础上加上weight权重得到的。

4.1.1 正向代理与反向代理

正向代理:是指客户端的代理,代表客户端向服务端发出网络请求,如图:

反向代理:是指服务端的代理,代表服务端向客户端发出响应结果,如图:

正向代理+反向代理,由客户端代理服务器代表Client客户机向服务端发出网络请求,由服务端代理服务器代表Server服务机向客户端发出响应结果。如图:

最最最简单的理解,正向代理和反向代理这个知识是计算机网络中的一个知识,由于当前使用的http网络请求时基于请求-响应模式,服务端开启后被动等待,由客户端主动发起网络请求(get请求、post请求),服务端提供结果响应,然后客户端再一次主动发起网络请求,服务端再一次提供结果响应

客户端局域网LAN1中的代理服务器代表客户端主机,我们称之为正向代理,
服务端局域网LAN2中的代理服务器代表服务端主机,我们称之为反向代理

相关知识:Java开发工具——从代理上网到反向代理

4.2 Nginx实现反向代理

Nginx是如何实现反向代理的,我们一步步来分析。两个tomcat服务通过nginx反向代理,本例子使用三台虚拟机进行测试,

         nginx服务器:192.168.101.3

         tomcat1服务器:192.168.101.5

         tomcat2服务器:192.168.101.6 

如图:

配置Nginx——修改nginx安装目录/conf/nginx.conf(这里是/usr/local/nginx/conf/nginx.conf文件),如下:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

#配置一个代理即tomcat1服务器
upstream tomcat_server1 {
            server 192.168.101.5:8080;
        }
#配置一个代理即tomcat2服务器
    upstream tomcat_server2 {
            server 192.168.101.6:8080;
        }

#配置一个虚拟主机
    server {
        listen 80;
        server_name aaa.test.com;
        location / {
				#域名aaa.test.com的请求全部转发到tomcat_server1即tomcat1服务上
                proxy_pass http://tomcat_server1;
				#欢迎页面,按照从左到右的顺序查找页面
                index index.jsp index.html index.htm;
        }

    }

    server {
        listen 80;
        server_name bbb.test.com;

        location / {
				 #域名bbb.test.com的请求全部转发到tomcat_server2即tomcat2服务上
                  proxy_pass http://tomcat_server2;
                  index index.jsp index.html index.htm;
        }
    }
}

Nginx可以实现反向代理的功能,Nginx只做请求的转发,后台有多个http服务器提供服务,nginx的功能就是把请求转发给后面的服务器,决定把请求转发给谁,nginx提供算法。

4.3 Nignx实现负载均衡

上面讲过,Nginx实现负载均衡是在反向代理的基础上加上一个weight权重,这里一步步分析。nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器。

         nginx负载均衡服务器:192.168.101.3

         tomcat1服务器:192.168.101.5

         tomcat2服务器:192.168.101.55

         tomcat3服务器:192.168.101.6

         tomcat4服务器:192.168.101.66

如图:

配置Nginx——修改nginx安装目录/conf/nginx.conf(这里是/usr/local/nginx/conf/nginx.conf文件),如下:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

#配置一个代理即tomcat1服务器
upstream tomcat_server1 {
            server 192.168.101.5:8080   weight=1;
            server 192.168.101.55:8080  weight=2;

        }
#配置一个代理即tomcat2服务器
    upstream tomcat_server2 {
            server 192.168.101.6:8080  weight=2;
server 192.168.101.66:8080  weight=3;
        }

#配置一个虚拟主机
    server {
        listen 80;
        server_name aaa.test.com;
        location / {
				#域名aaa.test.com的请求全部转发到tomcat_server1即tomcat1服务上
                proxy_pass http://tomcat_server1;
				#欢迎页面,按照从左到右的顺序查找页面
                index index.jsp index.html index.htm;
        }

    }

    server {
        listen 80;
        server_name bbb.test.com;

        location / {
				 #域名bbb.test.com的请求全部转发到tomcat_server2即tomcat2服务上
                  proxy_pass http://tomcat_server2;
                  index index.jsp index.html index.htm;
        }
    }
}

最后,请求aaa.test.com,通过nginx负载均衡,将请求转发到tomcat应用服务器。通过观察tomcat的访问日志或tomcat访问页面即可知道当前请求由哪个tomcat服务器受理。

4.4 Nginx三大功能一起小结

一图小结——Nginx再也不复杂(http服务器+虚拟主机+负载均衡):

配置Nginx——修改nginx安装目录/conf/nginx.conf(这里是/usr/local/nginx/conf/nginx.conf文件),如下:

​#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

#配置一个代理即tomcat1服务器
upstream tomcat_server1 {
            server 192.168.101.5:8080   weight=1;
            server 192.168.101.55:8080  weight=2;

        }
#配置一个代理即tomcat2服务器
    upstream tomcat_server2 {
            server 192.168.101.6:8080  weight=2;
server 192.168.101.66:8080  weight=3;
        }

#配置两个虚拟主机
    server {
        listen 80;
        server_name aaa.test.com;
        location / {
				#域名aaa.test.com的请求全部转发到tomcat_server1即tomcat1服务上
                proxy_pass http://tomcat_server1;
				#欢迎页面,按照从左到右的顺序查找页面
                index index.jsp index.html index.htm;
        }

    }

    server {
        listen 80;
        server_name bbb.test.com;

        location / {
				 #域名bbb.test.com的请求全部转发到tomcat_server2即tomcat2服务上
                  proxy_pass http://tomcat_server2;
                  index index.jsp index.html index.htm;
        }
}
# http服务器
server {
        listen       81; # 监听的端口
        server_name  localhost; # 域名或ip
        location / {      # 访问路径配置
            root   /usr/local/nginx/index;# 根目录
            index  index.html index.htm; # 默认首页
        }
    }

}

五、实践——Nginx安装使用(以centos7.0为例)

nginx是C语言开发,本节演示centos7.0上安装Nginx

5.1 安装好Nginx

步骤一:安装好相关依赖(gcc、pcre、zlib、openssl)

(1)gcc

安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++

(2)pcre

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库:yum install -y pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

(3)zlib

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库:yum install -y zlib zlib-devel

(4)openssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库:yum install -y zlib zlib-devel

步骤二:下载linux版本nginx压缩包

由于各个用户所需要的nginx版本不一定相同,所有这里不再提供nginx安装包,用户在到nginx官网下载。

(本节使用截止2019/12/01最新nginx-1.16.1.tar.gz安装演示,其他版本安装相同)

步骤三:解压并配置nginx

解压nginx

tar -zxvf nginx-1.16.1.tar.gz

配置nginx

进入解压目录中:cd nginx-1.16.1.tar.gz

配置nginx:

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client \

--http-proxy-temp-path=/var/temp/nginx/proxy \

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

--http-scgi-temp-path=/var/temp/nginx/scgi

注意:此处将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

一定要新建这个目录 否则后面无法启动./nginx

步骤四:编译安装

编译:继续上面的目录下(即刚才配置nginx的解压目录下)输入命令  make

安装:继续上面的目录下(即刚才配置nginx的解压目录下)输入命令  make  install

步骤五:启动nginx并访问测试成功

启动nginx 

cd /usr/local/nginx/sbin/

./nginx

注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:

./nginx -c /usr/local/nginx/conf/nginx.conf

如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))

查询nginx进程:ps aux|grep nginx

若查到nginx进程,则已经启动成功

5.2 相关附加操作 

附1:关闭nginx

方式1,快速停止:

cd /usr/local/nginx/sbin

./nginx -s stop

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

方式2,完整停止(建议使用):

cd /usr/local/nginx/sbin

./nginx -s quit

此方式停止步骤是待nginx进程处理任务完毕进行停止。

附2:重启nginx

方式1,先停止再启动(建议使用):

对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。

如下:

./nginx -s quit

./nginx

方式2,重新加载配置文件:

当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

./nginx -s reload

附3:设置开机自启动nginx(这个建设设置,不用每次启动了)

输入 vi /etc/init.d/nginx  新建编辑文件(输入下面的代码)

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

输入 :wq  保存并退出

输入  chmod a+x /etc/init.d/nginx   使vi生效

vi /etc/rc.local

加入一行  /etc/init.d/nginx start    保存并退出,下次重启会生效。

成功标志  开机启动后,centos7浏览器上localhost成功(当然最小化安装没有浏览器,桌面安装可以用浏览器看一下)

若是没有成功还是要到   cd /etc/usr/local/nginx/sbin  ./nginx 启动

六、小结

本文先介绍nginx三大功能原理图与配置文件,然后介绍centos7上安装使用nginx,希望对读者有用。

天天打码,天天进步!

发布了177 篇原创文章 · 获赞 31 · 访问量 8万+

Guess you like

Origin blog.csdn.net/qq_36963950/article/details/103194070