nginx安装、配置文件

114 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第1天,点击查看活动详情

nginx干什么

  1. 正向代理 客户端/浏览器的请求发送到代理服务器、代理服务器将请求转发给目标服务器8caf69f16d5e44da9b432adc38eaea88.png

  2. 反向代理
    由代理服务器统一对外提供服务、暴露一个ip+端口,由代理服务器按照一定的策略分发对应的请求到目标服务器,客户端、浏览器无感知,不知道真正提供服务的服务器! 3f4c149e09344ed6ac04e6078936e11f.png

  3. 静态资源服务器 将js、html、css、picture.....

nginx安装

nginx安装包列表

nginx.org/download/

安装所需环境

yum -y install pcre  pcre-devel zlib  zlib-devel openssl openssl-devel

参数解读:

编译需要gcc环境

yum install gcc-c++

pere正则表达式 pcre-devel是使用pcre二次开发的

yum install -y pcre pcre-devel

压缩、解压缩环境

yum install -y zlib zlib-devel

http、https支持

yum -y install openssl openssl-devel

下载安装包,最新版本

wget https://nginx.org/download/nginx-1.9.13.tar.gz

解压安装包

tar -zxvf nginx-1.9.13.tar.gz

配置、安装(进入解压目录)

# 配置
./configure --prefix=/usr/local/nginx
## 编译
make
## 安装
make install

测试、启动、重载配置

cd /usr/local/nginx/sbin

# 测试
./nginx -t
# 启动
./nginx
# 指定配置文件启动(不指定就是默认的)
./nginx -c /usr/local/nginx/conf/nginx.conf
# 重载配置
./nginx -s reload
# 粗暴停止(查询ID直接杀掉进程)
./nginx -s stop
# 优雅停止(等进程处理完任务之后停止)
./nginx -s quit

查看nginx

whereis nginx

验证测试

nginx安装成功、启动成功、访问你的ip+port 本地安装访问 http://localhost:80

配置文件

  1. 配置文件目录
/etc/nginx.conf
/etc/nginx/nginx.conf
  1. 配置文件结构
worker_processes  1; #线程数

events {
    worker_connections  1024; #连接数
}


http {
    server {
        listen       80; # 指定监听端口号
        server_name  localhost; # 指定监听IP地址
        
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        ......
    }
    server {
        listen       8080;
        server_name  localhost;
        
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}