Nginx使用手册
Nginx是什么?
nginx是一款轻量级、高性能的开源web服务器、反向代理服务器、邮件服务代理服务器。
反向代理是指,将客户端的请求转发到内网的多台服务器上,并将服务器的响应结果返回给客户端。以达到隐藏内部服务器信息、负载均衡的作用。
在linux下安装Nginx
在linux下安装Nginx有两种方式,一是通过包管理软件安装,二是通过源码编译安装。
包管理器安装
Centos
在centos内核的服务器上,使用如下命令下载安装。
sh
# 先更新现有的软件包
sudo yum update -y
# 使用 yum 包管理器来安装 Nginx。
sudo yum install nginx -y
# 安装完成后,启动 Nginx 服务。
sudo systemctl start nginx
# 设置开机启动
sudo systemctl enable nginx
# 查看nginx运行状态
sudo systemctl status nginxUbuntu
在ubuntu服务器上安装,使用如下命令下载安装。
sh
# 先更新apt以及现有的软件包
sudo apt update
sudo apt upgrade -y
# 使用 apt 包管理器来安装 Nginx。
sudo apt install nginx -y
# 安装完成后,启动 Nginx 服务。
sudo systemctl start nginx
# 设置开机启动
sudo systemctl enable nginx
# 查看nginx运行状态
sudo systemctl status nginx源码编译安装
sh
# 创建nginx专用用户
# -s /bin/false nginx 用于禁止用户登录
sudo groupadd nginx
sudo useradd -r -g nginx -s /bin/false nginx
# 安装编译依赖
## Debian系
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
## RedHat系
sudo yum groupinstall "Development Tools"
sudo yum install pcre-devel zlib-devel openssl-devel
# 下载nginx源码,并解压
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 配置编译选项
# --prefix指定nginx的安装目录。
# --user和--group指定用户和组。
# --with-http_ssl_module:启用 SSL 模块。
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module
# 编译并安装
make
sudo make install
# 修改目录权限 仅nginx
sudo chown -R nginx:nginx /usr/local/nginx
# 之后可以设置为以服务形式启动或直接运行nginx启动
/usr/local/nginx/sbin/nginx
Nginx可执行文件
通过通过which命令查找可执行文件的位置
sh
which nginx
# nginx可执行文件通常安装在/usr/sbin/nginx常用命令
sh
# 启动nginx,它会根据 ./conf/nginx.conf 配置文件中的设置来运行。
./sbin/nginx
# 启动nginx前检查配置文件是否存在语法错误
nginx -t
# 自定义配置文件路径
nginx -c /custom_path/custom.conf
# 立刻停止nginx进程
nginx -s stop
# 优雅停止nginx进程
nginx -s quit
# 不重启的情况下,重新加载配置
nginx -s reloadNginx配置
配置目录
nginx的配置文件目录包含如下文件: 
- nginx.conf:这是 Nginx 的核心配置文件,包含了全局配置指令和对其他配置文件的引用。
- conf.d:这个目录用于存放虚拟主机的配置文件,每个虚拟主机可以有一个单独的 .conf 文件。例如,你可以在这个目录下创建一个 example.com.conf 文件来配置一个名为 example.com 的虚拟主机。
- default.d:用于存放一些额外的配置片段。
配置项
conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
# 指定nginx工作进程的为nginx
user nginx;
# 设置nginx的工作进程数量为自动模式,将根据服务器配置自动设定进程数量
worker_processes auto;
# 设置错误日志目录
error_log /var/log/nginx/error.log;
# 设置进程pid保存的文件路径
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# 设置动态加载模块的配置文件路径
include /usr/share/nginx/modules/*.conf;
events {
# 设置每个 Nginx 工作进程可以同时处理的最大连接数为 1024
worker_connections 1024;
}
http {
# 定义日志格式,命名为main
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;
tcp_nopush on;
tcp_nodelay on;
# 设置长连接超时时间为 65 秒
keepalive_timeout 65;
# 设置 MIME 类型哈希表的最大大小为 2048
types_hash_max_size 2048;
# 该文件定义了各种文件扩展名对应的 MIME 类型
include /etc/nginx/mime.types;
# 设置默认的 MIME 类型为 application/octet-stream
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
# 包含其他虚拟主机或其他HTTP配置
include /etc/nginx/conf.d/*.conf;
server {
# 监听 ipv4和ipv6的80端口
listen 80 default_server;
listen [::]:80 default_server;
# 用于设置服务器域名,_表示匹配所有请求的域名
server_name _;
# 设置该服务器块的根目
root /usr/share/nginx/html;
# Load configuration files for the default server block.
# 包含该目录下以.conf结尾的配置文件
include /etc/nginx/default.d/*.conf;
# 一个匹配所有请求的 location 块
location / {
}
# 定义状态码为 404,返回的页面
error_page 404 /404.html;
# 精准匹配40x的页面
location = /40x.html {
}
# 定义状态码为 500、502等时,返回的页面
error_page 500 502 503 504 /50x.html;
# 精准匹配50x的页面
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}