Nginx

来自 Alpine Linux

Nginx (engine x) 是一个 HTTP 和反向代理服务器、邮件代理服务器以及通用 TCP/UDP 代理服务器

安装

Nginx 软件包在 Alpine Linux 仓库中可用。要安装它,请运行

apk update apk add nginx

为 Nginx 创建新用户和组 'www'

adduser -D -g 'www' www

为 HTML 文件创建目录

mkdir /www chown -R www:www /var/lib/nginx chown -R www:www /www

配置

你可能需要在编写自己的 nginx.conf 文件之前备份原始文件

mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig

配置 Nginx 监听端口 80 并处理 .html 或 .htm 文件

vi /etc/nginx/nginx.conf

user                            www;
worker_processes                auto; # it will be determinate automatically by the number of core

error_log                       /var/log/nginx/error.log warn;
#pid                             /var/run/nginx/nginx.pid; # it permit you to use rc-service nginx reload|restart|stop|start

events {
    worker_connections          1024;
}

http {
    include                     /etc/nginx/mime.types;
    default_type                application/octet-stream;
    sendfile                    on;
    access_log                  /var/log/nginx/access.log;
    keepalive_timeout           3000;
    server {
        listen                  80;
        root                    /www;
        index                   index.html index.htm;
        server_name             localhost;
        client_max_body_size    32m;
        error_page              500 502 503 504  /50x.html;
        location = /50x.html {
              root              /var/lib/nginx/html;
        }
    }
}

示例页面

vi /www/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>HTML5</title>
</head>
<body>
    Server is online
</body>
</html>

控制 Nginx

启动 Nginx

安装完成后,Nginx 尚未运行。要启动 Nginx,请使用 start

rc-service nginx start

你将收到关于状态的反馈。

 * Caching service dependencies ...                     [ ok ]
 * /run/nginx: creating directory
 * /run/nginx: correcting owner                         
 * Starting nginx ...                                   [ ok ]

测试配置

当你对 Nginx 配置文件进行任何更改后,你应该在重启/重新加载 Nginx 之前检查错误。
这将检查任何重复配置、语法错误等。要执行此操作,请运行

nginx -t

你将收到反馈,告知是否失败。如果一切正常,你将看到以下内容,然后可以继续重新加载 Nginx 服务器。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载和重启 Nginx

在向 Nginx 发送重新加载配置的命令或重启 Nginx 之前,配置文件中所做的更改将不会应用。
重新加载将执行配置的“热重载”,而不会导致服务器停机。它将使用新配置启动新的工作进程,并优雅地关闭旧的工作进程。如果你有待处理的请求,那么这些请求将在旧的工作进程终止之前由它们处理,因此这是一种非常优雅的重新加载配置的方式。如果你想重新加载 Web 服务器,请使用 reload

rc-service nginx reload

如果你想重启 Web 服务器,请使用 restart

rc-service nginx restart

停止 Nginx

如果你想停止 Web 服务器,请使用 stop

rc-service nginx stop

运行级别

通常,你希望在系统启动时启动 Web 服务器。这通过将 Nginx 添加到所需的运行级别来完成。

rc-update add nginx default

现在,当你下次启动计算机时,Nginx 应该会自动启动。要测试这一点,请运行

reboot

要确保 Nginx 已启动,请运行

ps aux | grep nginx

你应该得到类似这样的结果

  263 root       0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  264 www        0:00 nginx: worker process
  310 root       0:00 grep nginx

测试 Nginx

本节假设 Nginx 正在运行并且已创建示例 HTML 页面 "/www/index.html"。启动 Web 浏览器并将其指向你的 Web 服务器。你应该得到

Server is online

故障排除

如果 Nginx 未启动,请检查 Nginx 日志文件

less /var/log/nginx/error.log

确保配置文件不包含错误。如果存在任何错误,请编辑该文件。

nginx -t vi /etc/nginx/nginx.conf

Nginx 与 PHP

配置 Nginx 与 PHP5
配置 Nginx 与 PHP7
配置 Nginx 作为反向代理与 acme (Let's Encrypt)