开发者学堂课程【Linux Web 服务器 Nginx 搭建与配置:nginx 基本配置】学习笔记,与课程紧密联系,让用户快速学习知识.
课程地址:https://developer.aliyun.com/learning/course/579/detail/7989
nginx 基本配置(二)
五、nginx的安装
1.官方:
http://nginx.org/packages/centos/7/x86 64/RPMS
2.Fedora-EPEL:
https://mirrors.aliyun.com/epel/7/x86 64/
3.编译安装:
- yum install pcre-devel openssl-devel zlib-deve
- useradd -r nginx
- ./configure--prefix=/usr/local/nginx—conf--
error-log-
p
ath=/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--user=nginx --group=nginx--with-http_ssl_module
--with-http_v2 module --with-http dav module --with-http_stub_status_module --with-threads --with-file-aio
make && make install
以下为稳定版
Pre-Built Packages for Stable version
To set up the yum repository for RHEL/CentOS, create the file named /etc/tum.repos.d/nginx.repo with the following contents:
[nginx]
Name=nginx repo
Baseurl=http
:
//nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
Replace“os”with “rhel” or “centas”, depending on the distribution used, and“OSRELEASE”with "6” or “7”.
f
or 6.x or 7.x versions, respectively
For Debian/Ubuntu, in order to authenticate the nginx repository signature and to eliminate warnings about missing PGP key during installation of the nginx package, it is necessary to add the key used to sign the nginx packages and repository to the apt program keyring. Please download this key from our web site, and add it to the apt program keyring with the following command:
sudo apt-key add nginx_signing.key
For Debian replace codename with Debian distribution cedename, and append the following to the end of the /etc/apt/sourcea.list file:
deb http://nginx.org/packages/debian/ codename nginx
deb-src http://nginx.org/packages/debian/ codename nginx
For Ubuntu replace codename with Ubuntu distribution codename, and append the following to the end of the /etc/apt/sources, list file;
deb http://nginx.org/packages/ubuntu/ codename nginx
deb-src http://nginx.org/packages/ubuntu/ codename nginx
For Debian/Ubun
路径中可进行适当替换
例
【Ngink】
name=nginx repo
bseur1=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
此时为已读取效果
下面按直接用压路源进行安装,其中/etc/nginx/nginx.conf为主要配置文件,之后也是对其进行修改。接下来配置nginx,
[root@centos7~]#
[root@centos7~]#
[root@centos7~]#which nginx
usr/sbin/nginx
[root@centos7~]#nginx=-help
nginx:invalid ootiont:”-“
[root@centos7~]#nginx_j
Nginx:inva;id option:”
[root@centos7~]#nginx-h
nainx version:nginx/1.12.2
Usage:ngink ThVVtTqI L-Ssigna1] [-c filename] [-p prefix] [-gdirectives]
Options:
-?.-h :this help
-V:show version and exit
-v :show versionandconfioureoptions then exit
-T:test confiouration and exit
-t :test Cestcontiquration . dump it and exit
s signal :send signal to a master process : stop quit reopen reload
-p prefix :set prefix path (default:/usr/share/nginx/)
-cfilename:set configuration file (default:/ect?nginx/nginx.conf)
-g directives :set global directives out of configuration file
我们一般用[ront@centos7~]#nginx -s stop这个来启动期服务,但用过后要使用相应的进行停止
六、nginx 目录结构和命令
Is /usr/local/nginx/
html是测试页,sbin是主程序
Is /usr/local/nginx/sbin/
nginx 只有一个程序文件
ls /usr/local/nginx/html/
50x.html index.html 测试网页
Nginx:默认为启动nginx
-h查看帮助选项
-V 查看版本和配置选项
-t 测试nginx语法错误
-c filename 指定配置文件
(default:/etc/nginx/nginx.conf)
-s signal 发送信号给master进程,
signal : stop, quit, reopen,reload
示例:nginx -s stop 停止nginx
nginx -s reload 加载配置文件
-g directives在命令行中指明全局指令
七.nginx 配置
1. 配置文件的组成部分
主配置文件:nginx.conf
子配置文件 include conf.d/t.conf
fastcgi,uwsgi,scgi等协议相关的配置文件mime.types:支持的mime类型
主配置文件的配置指令:
directive value [value2...];
注意:
(1)指令必须以分号结尾
(2) 支持使用配置变量
内建变量:由Nginx模块引入,可直接引用自定义变量:由用户使用set命令定义
set variable_name value;
内置模块很多,可以参考官网,之后具体讲述
2.Main-全局配置段常见的配置指令分类
u
s
er nginx;
Worker processes auto:
error -log/var/log/nginx/ercor.log;
pid /run/nginx.pid
#Load dynamic modules.See/usr/share/nginx/README.dynanic
Events {
worker -connections 1024
{
这是一个全局式的设置,它影响的是所有服务,http服务、邮件服务、转化代理服务……
正常运行必备的配置
优化性能相关的配置
用于调试及定位问题相关的配置
事件驱动相关的配置
帮助文档
http://nginx.org/en/docs/
正常运行必备的配置:
帮助文档:http://nginx.org/en/docs/ngx_core_module.html
1. user
Syntax: user user [group];
Default: user nobody nobody;
Context: main
指定
worker进程的运行身份,如组不指定,默认和用户名同名
2、pid /PATH/TO/PID FILE
指定存储nginx主进程PID的文件路径
3.include file I mask
指明包含进来的
其它配置文件片断
4.load_module file
模块加载配置文件:/usr/share/nginx/modules/*.conf
指明要装载的动态模块路径:/usr/lib64/nginx/modules
八、nginx 配置文件
主配置文件结构:四部
main block
(主块
):主配置段,即全局配置段,对http,mail都有效
event {
…
}
事件驱动相关的配置
http
{
…
} http/https 协议相关配置段
mail {
…
} mail 协议相关配置段
stream {
…
}
stream 服务器相关配置段
官方目前提供的只有main block和HTTP这两大块,其余需要自己手动填写。
九、http 协议相关的配置结构
http {
…
…各server的公共配置
server { 每个server用于定义一个虚拟主机
…
}
server {
…
servername 虚拟生机名
root 主目录
alias 路径别名
location [OPERATOR] URL { 指定URLB的特性
…
if CONDITION I
…
}
}
}
}