---
title: "【Nginx一】Nginx安装与总体介绍"
author: "bit1129"
date: "2014-09-10T01:08:12Z"
tags: []
categories: ["开源软件"]
slug: "nginx-nginx"
draft: false
lastmod: "2014-09-10T22:32:56Z"
id: 2114501
url: "https://bit1129.iteye.com/blog/2114501"
---

## 启动、停止、重新加载Nginx

```
nginx            启动Nginx服务器，不需要任何参数u
nginx -s stop    快速(强制)关系Nginx服务器
nginx -s quit    优雅的关闭Nginx服务器
nginx -s reload  重新加载Nginx服务器的配置文件
nginx -s reopen  重新打开Nginx日志文件
```

 

- nginx -s quit 所谓优雅的关闭Nginx服务器是指等待所有的工作者进程处理还没处理完的请求之后再进行关闭
- **问题： reopen重新打开Nginx日志文件表示什么含义?意义在哪？即，这个命令产生的影响是什么？**

 

## 主进程和工作进程

 Nginx的工作模式是多进程工作模式，它分为一个主进程和一组工作者进程，主进程负责维护配置文件以及管理工作者进程。工作者进程负责响应请求，可以在主配置文件nginx.conf(一般位于/usr/local/nginx/conf)文件中设置工作进程数，工作进程数通常设置为跟物理机的CPU核数相同，如下设置工作进程数是1

 

```
worker_processes  1;
```

 

## 启动Nginx服务器

- 使用nginx启动Nginx服务器

<!--THE END-->

- 通过ps -ef|grep nginx可以得到如下的进程信息

 

```
root     31323     1  0 22:20 ?        00:00:00 nginx: master process ./nginx
nobody   31324 31323  0 22:20 ?        00:00:00 nginx: worker process
tom  31329 13070  0 22:21 pts/0    00:00:00 grep --color=auto nginx
```

 

**说明：**

- 此时，worker process的进程所有所有者是nobody，而master process的所有者是root，tom是当前用户。nobody是在主配置文件中设置，如果不设置就是nobody

```
#user  nobody;
```

- 工作者进程是主进程的子进程

 

## 主进程PID(nginx.pid)

当Nginx启动后，Nginx主进程的id号写入nginx.pid文件，nginx.pid文件所在的目录是/usr/local/nginx/logs。这个id号在Nginx重新加载配置时用到，通过它，Nginx可以kill它所有的子进程(即工作者进程)，然后重新启动

 

## Nginx配置文件的结构

Nginx同Apache一样，它提供的功能是由模块(Modules)组成的，而这些模块是由配置文件中的指令(Directive)控制的。Nginx中，指令分为两类，一类是简单指令，一类是块指令

- 简单指令(Simple Directive)

       简单指令有指令名和几个参数组成，它们之间用空格隔开，最后以分号结束。比如下面的内容中，include，default\_type都是简单指令名

      

```
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;
```

  

 

- 块指令(Block Directive)

       块指令和简单指令有相似的结构，简单指令是以分号结尾，而块指令是以{}括起来的一组操作指令(instructions) **举例说明？？下面的例子其实是上下文**

       

```
        location / {
            root   html;
            index  index.html index.htm;
        }
```

    

 

- 上下文(Context)

      如果块指令结尾的{}包含其他的指令，那么这个块指令称之为上下文。比如event、http、location和server。在配置文件中，每个Context都有一个父级Context，顶级的event和http context的父context称为main(main不是实际存在的Context)，而server的父级context是http，location的父级context是server。