Nginx配置和路由
勿以浮沙筑高台
Nginx
- Nginx安装
- Nginx配置文件
- Nginx.conf基本结构
- Nginx.conf配置文件解析
- Nginx虚拟主机配置
- location匹配规则
- = 开头表示精准匹配
- 最长匹配原则
Nginx
高性能web服务器
Nginx安装
1.下载Nginx
Nginx下载地址:http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.18.0.tar.gz
2.安装Nginx需要的依赖环境
因为nginx是C语言编写的因此需要c语言环境
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
3.解压
tar xf nginx-1.18.0.tar.gz
4.配置编译路径
cd nginx-1.18.0/
./configure --prefix=/usr/app/nginx
5.编译和安装
make & make install -j 4
cd /usr/app/nginx
5.启动
cd sbin
ps -ef | grep nginx
6.验证
访问linux服务器地址出现下图代表成功
Nginx配置文件
cd /usr/app/nginx/conf/
vim nginx.conf
Nginx.conf基本结构
// nginx全局块
...
// events块
events {...
}
// http 块
http {// http全局块...// server块server {...}// http全局块...
}
// upstream 块
upstream {}
Nginx.conf配置文件解析
# 配置nginx的用户组 默认为nobody
#user nobody;
#指定工作进程的个数
#默认是1个,具体可以根据服务器cpu数量进行设置,如果不知道cpu的数量,可以设置为auto
worker_processes auto;# 配置nginx的错误日志 格式为 log路径 log级别
# error_log 的日志级别为: debug info notice warn error crit alert emerg 紧急由低到高
# error_log的默认日志级别为error,那么就只有紧急程度大于等于error的才会记录在日志
# error_log 的作用域为 main http mail stream server location#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#Nginx运行文件地址
#pid logs/nginx.pid;#工作模式及连接数上限
events {worker_connections 1024;
}http {# 文件扩展名和文件类型映射表 # mime.types 指text/css,image/jpeg,text/html这样的包含类型。# 在具mimetypes文件里能看见具体结构有哪些include mime.types;# 默认文件类型default_type application/octet-stream;# log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';// 访问日志配置路径// main# access_log logs/access.log main;# 是否开启0拷贝sendfile on;# 是否 #减少网络报文段的数量#tcp_nopush on;#keepalive_timeout 0;# 链接超时时间 默认 65s keepalive_timeout 65;# 开始gzip压缩,降低带宽使用和加快传输速度,但增加了CPU的使用#gzip on;server {# 端口号80listen 80;# 域名 Ip,一级域名,二级域名配置server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;# 对请求的路由进行过滤 正则匹配location / {root html;index index.html index.htm;}# 报错地址#error_page 404 /404.html;# redirect server error pages to the static page /50x.html## 报错页面配置error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}
}
Nginx虚拟主机配置
更改linux文件的hosts
vim /etc/hosts
#格式 : IP 域名
192.168.31.5 www.fu.com
192.168.31.5 www.yan.com
因为用的虚拟机,更改本机hosts
在Nginx.conf中配置虚拟机
server {listen 80;server_name www.fu.com; # 域名区分location / {root html/fu;index index-1.html;}
}
server {listen 80;server_name www.yan.com; # 域名区分location / {root html/fu;index index-2.html;}
}
在nginx.conf配置虚拟主机文件路径
include "vhosts/server.demo.conf";
创建虚拟主机文件路径
cd /usr/app/nginx/conf
mkdir -p vhosts
cp /usr/app/nginx/conf/nginx.conf /usr/app/nginx/conf/vhosts/server.demo.conf
然后将上面的server信息放进去。
创建server信息里的html文件
cd /usr/app/nginx/html/
echo "www.fu.com" >> index-1.html
echo "www.yan.com" >> index-2.html
重新加载配置文件并启动
./nginx -s reload./nignx -t #启动
查看是否启动成功
进入log/error.log查看是否有signal process started这句话,有则代表成功
访问网站http://www.fu.com/
,http://www.yan.com/
成功
location匹配规则
= 开头表示精准匹配
# ~ 大小写敏感
# ~* 忽略大小写
# ^~ 只需匹配uri开头
# @ 定义一个命名的 location,在内部定向时使用,例如 error_page
location [ = | ~ | ~* | ^~ ] /uri/ { ... }
location @name { ... }
任意匹配 location / {}
当找不到路由的时候就会走这个路由
server {listen 80;server_name www.fu.com; # 域名区分location / {root html/fu;index 404.html;}
}
只能匹配=号后面的内容。
// 精准匹配location = / {root html/fu;index 404.html;}
// ~* 忽略大小写location ~* / {root html/fu;index 404.html;}
// ~大小写敏感location ~ / {root html/fu;index 404.html;}
最长匹配原则
就是当找不到路由的时候,会按最长路径的路由进行匹配
比如:
location /123 {root html/fu;index 404.html;}location /1234 {root html/fu;index 404.html;}#动静分离配置location ~* \.(gif|css|js|png|jpg|jpeg){root html/fu;index 404.html;}
我们访问1234598734时,没有这个路由则会匹配到1234这个路由,按匹配字符数量最多的进行路由。
Nginx配置和路由相关推荐
- Nginx For Windows 路由配置
Nginx For Windows 路由配置 一.路由配置说明 二.需求说明 三.配置文件 一.路由配置说明 使用Nginx进行路由配置. 使用过 SpringCloud 网关的同学都知道,网关可以使 ...
- vue路由history模式,nginx配置
nginx配置内容 # For more information on configuration, see: # * Official English Documentation: http://n ...
- 4.Nginx配置进阶(四)
Nginx配置 ============================================================================== 概述: 本章我们将继续 ...
- linux history nginx,vue-router History 本地开发环境和nginx配置
vue-router mode=history本地开发环境配置 解决方法1.修改webpack的的devServer配置项(devServe存在于,rvue-cli2在webapck.config.j ...
- 通过Nginx配置多域名访问
通过Nginx配置多域名访问 一.下载Nginx 访问Nginx官网选择对应版本和安装包:http://nginx.org/ 以下讲述为在Windows环境下配置Nginx进行多域名访问 二.Ngin ...
- nginx 配置详解_Nginx 配置详解
序言 Nginx是lgor Sysoev为俄罗斯访问量第二的http://rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HT ...
- nginx配置 vue打包后的项目 解决刷新页面404问题|nginx配置多端访问
访问vue页面时,/# 使url看着不美观,使用 H5 history模式可以完美解决这个问题,但需要后端nginx帮助.接下来我们自己配置一下. 使用前端路由,但切换新路由时,想要滚动到页面顶部,或 ...
- Nginx配置示例文件
Nginx配置示例文件 nginx.conf worker_processes 8;events {worker_connections 1024; }http {include mime.types ...
- NGINX配置基于Node.js服务的负载均衡服务器
NGINX配置基于Node.js服务的负载均衡服务器 本部署指南说明了如何使用NGINX开源和NGINX Plus在Node.js应用程序服务器池之间平衡HTTP和HTTPS通信.本指南中的详细说明适 ...
最新文章
- android studio 的AVD Manager点击无响应解决办法
- Github Star 11.5K项目再发版:AAAI 2021 顶会论文开源,80+多语言模型全新升级
- 深入理解计算机系统(2.3)---整数的表示方式精解无符号与补码编码(重要)...
- hdu4699-Editor【对顶栈】
- 使用七牛云对网站进行加速基本配置
- linux shell脚本判断文件行数,判断文件是否存在的shell脚本代码
- 科研人看过来!2021腾讯AI Lab犀牛鸟专项研究计划开始申请
- LeetCode开心刷题二十七天——51. N-Queens
- butterknife 不能绑定错误
- JDK8的新特性——Lambda表达式
- 2017.0622.《计算机组成原理》-虚拟存储器和主存
- 【python】小游戏程序——弹跳小球
- 千亿数仓第四章(指标计算_订单分析地域、分类维度分析业务开发)
- npm 安装参数中的 --save-dev 是什么意思
- 推荐个不错的 Word 全文翻译和压缩工具!
- matlab输入syms有错,matlab的问题:当我输入 syms x;f=x*x 按回车后就出错,这是怎么回事啊?请大家帮帮...
- 深信服 一面 2018 秋招
- linux 查看当前状态_Linux视频编辑的当前状态2018
- 中国软件服务外包IT公司最新排名-IT外包最强前50名
- 打开App:微信浏览器内部打开app跳转