目录
Nginx 是一个高性能的HTTP和反向代理服务器 .随着更多的前后端分离的项目启动就会更多的使用nginx。
1. 简介
Nginx 是一个高性能的HTTP和反向代理服务器 .随着更多的前后端分离的项目启动就会更多的使用nginx。
2.下载nginx方式
2.1 wget 方式
wget -c https://nginx.org/download/nginx-1.11.6.tar.gz
2.2 官网下载到本地
2.3 解压
tar -zxvf nginx.tar.gz -C /usr/local/nginx
3. 前提环境安装
3.1安装gcc 环境
yum install gcc-c++
3.2安装PCRE依赖库
yum install -y pcre pcre-devel
3.3 安装zlib 依赖库
yum install -y zlib zlib-devel
3.3安装OpenSSL安全套接字层密码库
yum install -y openssl openssl-devel
4.nginx安装
tar -zxvf nginx.tar.gz -c /usr/local/
cd /usr/local/nginx
./configure --prefix=/usr/local/nginx #指定nginx配置路径为默认路径
./configure --prefix=/usr/local/nginx --with-http_ssl_module #增加https模块
make
make install
#安装后,可以看到 /usr/local/nginx/sbin
5.nginx配置
5.1 匹配规则
1.规则说明
location = / {
# 完全匹配 =
# 大小写敏感 ~
# 忽略大小写 ~*
# 以s开头 ^s
}
2.例子
location ^~ /images/ {
# 前半部分匹配 ^~
# 可以使用正则,如:
# location ~* \.(gif|jpg|png)$ { }
}
location / {
# 如果以上都未匹配,会进入这里
}
location ^~ /report/gateway {
# 以/report/gateway开头,区分它的大小写
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif,jpg或jpeg 结尾的请求
# 不区分大小写
}
5.2.例子说明
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
# 3.如果访问 linkkb-server就相当于访问127.0.0.1:8082
upstream linkkb-server {
server 127.0.0.1:8082;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#1.localhost或者127.0.0.1 访问就会进入目录下,访问index.html或者index.htm
location / {
root D:/coderfarmer/codeing/link_client_server/linkkb-web/linkkb-web/html;
index index.html index.htm;
}
#2.如果是以/linkkb结束的就 访问 linkkb-server
location ^~ /linkkb {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://linkkb-server;
}
}
}
# 代码中注释序号为执行顺序
6. 启动停止测试
- 启动 重启
./nginx
./nginx -r reload
2. 停止
./nginx -s stop
3.测试nginx
访问http://127.0.0.1:80