Nginx整理

2020-05-21  本文已影响0人  水煮鱼又失败了

目录

[TOC]

1 安装

1.1 安装依赖项

yum install gcc pcre-devel zlib-devel

1.2 下载

wget http://nginx.org/download/nginx-1.17.9.tar.gz

1.3 安装

#生成Makefile文件
./configure
#编译
make
#安装
make install

安装成功后,nginx目录如下:

/usr/local/nginx

目录下结构如下:

conf html logs sbin

2 nginx操作命令

2.1 启动

/usr/local/nginx/sbin/nginx

启动后,可以查看系统中对应的线程:

[root@localhost sbin]# ps -ef |grep nginx
root      9708     1  0 08:41 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    9709  9708  0 08:41 ?        00:00:00 nginx: worker process

浏览器访问对应服务器的80端口地址(如http://localhost),网页显示如下内容,表示启动成功:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

2.2 重新加载配置文件

/usr/local/nginx/sbin/nginx -s reload

执行命令后,主进程校验配置文件有效性,并尝试应用新的配置,如成功,主进程将启动新的工作进程并向旧的工作进程发送关闭消息,旧进程收到消息后,不再接收新的连接请求,并继续为当前请求提供服务,直到所有请求都得到服务

如新配置存在问题,无法使用,则主进程将回滚更改并继续使用旧的配置。

总结:

执行重新加载命令,如加载失败,仍采用之前配置;如加载成功,则新的请求采用新的配置,之前未结束的请求,仍然采用之前的配置。

2.3关闭

命令需在启动nginx的用户下执行。

/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s stop

2.4 校验配置文件

/usr/local/nginx/sbin/nginx -t

输出如下:

[root@localhost conf]# /usr/local/nginx/sbin/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

/usr/local/nginx/sbin/nginx -T

输出如下:

[root@localhost conf]# /usr/local/nginx/sbin/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

configuration file /usr/local/nginx/conf/nginx.conf:
......(以下省略,为配置文件内容)

3 配置

3.1 说明

nginx配置文件为核心部分,相当于一个微型语言。

配置文件分为命令配置块配置

nginx配置,存在配置的继承关系,如子块中没有配置,父块中配置了,子块中将继承父块中的配置。

每个块和命令存在以下结构关系:

...#全局块

events{#events块
    ...
}

http{#http块
    ...#http全局块
    server{
        ...#server全局块
        location [PATTERN] {#location块
            ...
        }
        location [PATTERN] {
            ...
        }
    }
    server{
        ...
    }
}


其中events块、http块有一个,server块、location块可有多个。

3.1 基本配置

  1. server块默认配置

注释掉原有配置文件中server块。新增

server {
        location / {
           root html;
           index index.html;
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读