nginx server 如何写,如何写 nginx 配置
本篇文章主要介绍 nginx server
虚拟服务器如何配置。其中包括的一些例子我会已保存到 nginx 。尽可能的想要整理一份比较完整的配置说明,避免找寻资料的麻烦。博主也尽可能的保证本篇文章的准确性,如有失误,请告知。
通过 $ nginx -V
,你可以看到 nginx 的编译配置信息:
$ nginx -V
nginx version: nginx/1.17.3
built by gcc 8.3.0 (Debian 8.3.0-6)
built with OpenSSL 1.1.1c 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads ...
其中可以看到 --prefix=/etc/nginx
,nginx 安装时会把相关数据文件写入到该目录,如我们的配置文件 --conf-path
。
每次更改 nginx 的配置文件,你需要执行一下操作:
# 验证配置文件的正确性
$ nginx -T
# 重新加载配置文件
$ nginx -s reload
基础概念
这里推荐 nginx 的官方文档地址:http://nginx.org/en/docs/。
文档中涵盖了各个模块的配置用法,以及默认值,可以填写的上下文位置。
目前 nginx 支持多种服务类型:
http
mail
stream
google perftools
我们 着重介绍 http 服务。其它服务基本知识点都能涵盖到。
安装完 nginx ,我们先来看一看 nginx 的默认配置 /etc/nginx/nginx.conf
,当然可能与你的默认配置不同,不过大同小异:
# worker以什么身份运行
user nginx; // default nobody
# worker进程个数,一般为 CPU 个数,也可选 auto
worker_processes 1; # default 1
# 每个worker可打开的描述符限制
worker_rlimit_nofile 8192;
# 错误日志保存路径和级别
error_log /var/log/nginx/error.log warn;
# 进程pid保存路径
pid /var/run/nginx.pid;
# 指定dns服务器
resolver 10.0.0.1;
events {
# 每个worker最大连接数
worker_connections 1024; # default 1024
}
# http 服务定义
http {
# 加载 mime 类型
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函数(zero copy 方式)来输出文件,如果磁盘IO重负载应用,可设置为off
sendfile on;
# 此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
#tcp_nopush on;
keepalive_timeout 65;
# 代理相关设置
# proxy_connect_timeout...
剩余内容已隐藏