1、安装 nginx

Nginx 是一款免费、开源的、高性能的 HTTP 服务器和反向代理服务器,可以部署在 Linux、Windows 等操作系统上。安装 Nginx 的方法有很多,可以选择源码安装,也可以使用软件包管理工具安装,比如在 CentOS 上可以使用 yum 安装:

12
$ yum install nginx
$ systemctl start nginx
Bash

2、Nginx 配置文件

Nginx 的配置文件位于 /etc/nginx/nginx.conf,打开 nginx.conf 文件,可以看到如下内容:

123456
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}
nginx

3、Nginx 配置详解

user nginx; 表示 Nginx 进程运行的用户,一般默认使用 nginx 用户;worker_processes auto; 表示 Nginx 进程数量,auto 表示自动设置,一般为 CPU 核数;error_log /var/log/nginx/error.log; 表示 Nginx 的错误日志;pid /run/nginx.pid; 表示 Nginx 进程 ID 文件;events { worker_connections 1024; } 表示 Nginx 最大连接数,一般设置为 1024。

4、Nginx 配置实例

以下是一个实际的 Nginx 配置文件:

123456789101112131415161718192021
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log;
pid        /run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}
nginx