系统环境
CentOS Linux release 7.5.1804 (Core) mini 版安装系统
http://nginx.org/download/nginx-1.14.0.tar.gz
安装系统依赖包
- yum install zlib-devel
- yum install pcre-devel
- yum install gcc
- yum install openssl-devel
安装 nginx
解压源码包
- tar xzvf nginx-1.14.0.tar.gz
- cd nginx-1.14.0
编译安装
更多配置参数 http://nginx.org/en/docs/configure.html 信息
- ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre --with-http_gunzip_module --with-http_gzip_static_module --with-file-aio
- make
- make install
配置系统环境变量
方便执行程序
- echo 'export PATH="/usr/local/nginx/sbin:$PATH"'>> /etc/profile
- # 执行后, 使当前环境变量立即生效
- source /etc/profile
启动 nginx
- # 直接启动, 执行 nginx 就启动服务了
- nginx
验证结果
这里不讨论 firewalld 服务的配置, 所以直接关闭防火墙
- # 本次关闭防火墙
- systemctl stop firewalld
- # 设置下次开机不启动防火墙
- systemctl disable firewalld
打开浏览器, 输入服务器 IP, 就能正常访问页面了
修改配置后怎么办
- # 不关闭服务都情况下加载服务
- nginx -s reload
nginx 执行参数方式
nginx -s signal
signal(信号)有以下几种
stop - 快速关闭
quit - 正常退出程序
reload - 重新加载配置文件
reopen - 重新打开日志文件
如需要退出 nginx 服务
nginx -s quit
下面的方式等效, 其中 1628 为当前运行状态的 nginx 进程 ID,kill 向 nginx 发送 QUIT 信号
kill -s QUIT 1628
nginx 配置文件结构
外层指令分为
- events
- http
- events {
- }
- http {
- }
指令内的参数必须以; 分号结尾, 以 {} 花括号包裹
http 内部指令又包含 server 指令
- events {
- }
- http {
- server {
- }
- }
server 指令内部又包含 location 块
- events {
- }
- http {
- server {
- location / {
- }
- }
- }
静态文件配置方式
- location / {
- root /data/www;
- }
- location /images/ {
- root /data;
- }
完整的 server 结构样子
- server {
- location / {
- root /data/www;
- }
- location /images/ {
- root /data;
- }
- }
配置后要使配置生效, 需要重新加载服务
nginx -s reload
简单代理服务器配置方式
默认提供服务的配置,
- server {
- listen 8080;
- root /data/up1;
- location / {
- }
- }
反向代理配置
- server {
- location / {
- proxy_pass http://localhost:8080;
- }
- location /images/ {
- root /data;
- }
- }
支持正则匹配配置路由规则
- location ~ \.(gif|jpg|png)$ {
- root /data/images;
- }
完整的反向代理配置
- server {
- location / {
- proxy_pass http://localhost:8080/;
- }
- location ~ \.(gif|jpg|png)$ {
- root /data/images;
- }
- }
配置 FastCGI 代理
- server {
- location / {
- fastcgi_pass localhost:9000;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param QUERY_STRING $query_string;
- }
- location ~ \.(gif|jpg|png)$ {
- root /data/images;
- }
- }
更多配置请参考官方文档 http://nginx.org/en/docs/beginners_guide.html
来源: http://www.bubuko.com/infodetail-2694164.html