Nginx 配置文件在线生成: https://nginxconfig.io/
# 一、Nginx介绍
Nginx是一款面向性能设计的HTTP服务器,能反向代理HTTP,HTTPS和邮件相关(SMTP,POP3,IMAP)的协议链接。并且提供了负载均衡以及HTTP缓存。它的设计充分使用异步事件模型,削减上下文调度的开销,提高服务器并发能力。采用了模块化设计,提供了丰富模块的第三方模块。
所以关于 Nginx,有这些标签:「异步」「事件」「模块化」「高性能」「高并发」「反向代理」「负载均衡」
Linux系统:Centos 7 x64Nginx版本:1.11.5
# 二、安装
# 2.1 安装依赖
prce(重定向支持)和openssl(https支持,如果不需要https可以不安装。)
yum install -y pcre-devel
yum -y install gcc make gcc-c++ wget
yum -y install openssl openssl-devel
CentOS 6.5我安装的时候是选择的“基本服务器”,默认这两个包都没安装全,所以这两个都运行安装即可。
# 2.2 下载
wget http://nginx.org/download/nginx-1.13.3.tar.gz
wget http://nginx.org/download/nginx-1.13.7.tar.gz
# 如果没有安装wget
# 下载已编译版本
$ yum install wget
# 解压压缩包
tar zxf nginx-1.13.3.tar.gz
# 2.3 编译安装
然后进入目录编译安装,configure参数说明
cd nginx-1.11.5
./configure
....
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
安装报错误的话比如:
“C compiler cc is not found”,这个就是缺少编译环境,安装一下就可以了yum -y install gcc make gcc-c++ openssl-devel
如果没有error信息,就可以执行下边的安装了:
make
make install
# 2.4 nginx测试
运行下面命令会出现两个结果,一般情况
nginx会安装在/usr/local/nginx目录中
cd /usr/local/nginx/sbin/
./nginx -t
# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 2.5 设置全局nginx命令
vi ~/.bash_profile
将下面内容添加到
~/.bash_profile文件中
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin/
export PATH
运行命令
source ~/.bash_profile让配置立即生效。你就可以全局运行nginx命令了。
# 三、开机自启动
开机自启动方法一:
编辑
vi /lib/systemd/system/nginx.service文件,没有创建一个touch nginx.service然后将如下内容根据具体情况进行修改后,添加到nginx.service文件中:
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]:服务的说明Description:描述服务After:描述服务类别[Service]服务运行参数的设置Type=forking是后台运行的形式ExecStart为服务的具体运行命令ExecReload为重启命令ExecStop为停止命令PrivateTmp=True表示给服务分配独立的临时空间
注意:
[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
- 保存退出。
- 设置开机启动,使配置生效:
systemctl enable nginx.service
# 输出下面内容表示成功了
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
开机自启动方法二:
vi /etc/rc.local
# 在 rc.local 文件中,添加下面这条命令
/usr/local/nginx/sbin/nginx start
如果开机后发现自启动脚本没有执行,你要去确认一下
rc.local这个文件的访问权限是否是可执行的,因为rc.local默认是不可执行的。修改rc.local访问权限,增加可执行权限:
chmod +x /etc/rc.d/rc.local
# 四、运维
# 4.1 服务管理
# 启动
/usr/local/nginx/sbin/nginx
# 重启
/usr/local/nginx/sbin/nginx -s reload
# 关闭进程
/usr/local/nginx/sbin/nginx -s stop
# 平滑关闭nginx
/usr/local/nginx/sbin/nginx -s quit
# 查看nginx的安装状态,
/usr/local/nginx/sbin/nginx -V
关闭防火墙,或者添加防火墙规则就可以测试了
service iptables stop
或者编辑配置文件:
vi /etc/sysconfig/iptables
添加这样一条开放
80端口的规则后保存:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
重启服务即可:
service iptables restart
# 命令进行查看目前nat
iptables -t nat -L
# 4.2 重启服务防火墙报错解决
service iptables restart
# Redirecting to /bin/systemctl restart iptables.service
# Failed to restart iptables.service: Unit iptables.service failed to load: No such file or directory.
在
CentOS 7或RHEL 7或Fedora中防火墙由firewalld来管理,当然你可以还原传统的管理方式。或则使用新的命令进行管理。 假如采用传统请执行一下命令:
# 传统命令
systemctl stop firewalld
systemctl mask firewalld
# 安装命令
yum install iptables-services
systemctl enable iptables
service iptables restart
# 五、nginx卸载
如果通过
yum安装,使用下面命令安装。
yum remove nginx
- 编译安装,删除
/usr/local/nginx目录即可 - 如果配置了自启动脚本,也需要删除。
# 六、参数说明
# 七、配置
在
Centos默认配置文件在/usr/local/nginx-1.5.1/conf/nginx.conf我们要在这里配置一些文件。nginx.conf是主配置文件,由若干个部分组成,每个大括号{}表示一个部分。每一行指令都由分号结束;,标志着一行的结束。
# 7.1 常用正则
| 正则 | 说明 | 正则 | 说明 |
|---|---|---|---|
. |
匹配除换行符以外的任意字符 | $ |
匹配字符串的结束 |
? |
重复0次或1次 |
{n} |
重复n次 |
+ |
重复1次或更多次 |
{n,} |
重复n次或更多次 |
* |
重复0次或更多次 |
[c] |
匹配单个字符c |
\d |
匹配数字 | [a-z] |
匹配a-z小写字母的任意一个 |
^ |
匹配字符串的开始 | - | - |