当前位置:首页 > 服务器技术 > nginx

nginx安装配置

系统环境

安装系统依赖包

        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
    

编译安装

        ./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;
    }
}
    

更多配置请参考官方文档

原文:https://www.cnblogs.com/zengchunyun/p/9346226.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!