先吐槽一下~
平时很少接触 nginx,只有个模糊了解,知道是可以反向代理,负载均衡的作用,在以前公司只要交给运维,或者让运维人员帮忙配置一下,就ok了,我还抽了一段时间看了看nginx的官网和api文档,那真是每个字都认识,连起来就是都不明白~,果断放弃 现在觉得前端只要会简单的去配置一下代理就可以了
背景
本地nginx用的是nginx-1.19.8版本 找到本地nginx文件安装目录的\conf\nginx.conf,打开
代码展示
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
underscores_in_headers on;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 5000;#当前端口号
server_name localhost;#当前名称何以是ip也可以是域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {#配置项目路径root则是最上层目录的定义
root D:\\object\\yuezhu\\vueHuka\\dist\\;
index index.html index.htm;
}
location /hukaApp2/ {#alias是一个目录别名的定义 配置项目路径(跳转)
alias D:\object\yuezhu;
index index.html index.htm;
}
location /api/ {#proxy_pass 代理转发
proxy_pass http://192.168.1.123:8082/;
}
location /business/ {
proxy_pass http://192.168.1.123:8085/;
}
location /daike/ {
proxy_pass http://192.168.1.123:8088/;
}
location /pay/ {
proxy_pass http://192.168.1.123:8086/;
}
location /login/ {
proxy_pass http://192.168.1.123:8083/;
}
#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;
}
}
# 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;
# }
#}
}
代码说明(主要针对serve)
- serve{} //一个serve代表一个服务,比如一个本地项目的npm run dev启动的环境
- #代表注释 相当于js //
- listen //当前端口号
- server_name //当前名称何以是ip也可以是域名
- location //根据uri来进行不同的定位,在虚拟主机中是必不可少的,location可以定位网站的不同部分,定位到不同的处理方式
location代码块详细见解
- 语法匹配规则(会点正则规则的其实更好理解和使用)
| 标题 | 含义 |
|---|---|
| location = /uri | = 表示精确匹配,只有完全匹配上才能生效 |
| location ^~ /uri | ^~ 开头对URL路径进行前缀匹配,并且在正则之前。 |
| location ~ pattern | ~开头表示区分大小写的正则匹配 |
| location ~* pattern | 开头表示不区分大小写的正则匹配 |
| location /uri | 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后 |
| location / | 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default |
- root 配置项目路径root则是最上层目录的定义
- alias alias是一个目录别名的定义 配置项目路径(例如跳转)
- proxy_pass 代理转发
- index index.html index.htm; 默认首页
项目中遇到的问题及如何解决
vue 项目打包后nginx,页面显示404
- 发现:项目使用的是
history路由,所有nginx需要进行配置
location / {
root html;
index index.html;
……
try_files $uri $uri/ /index.html; ---解决页面刷新404问题
}
注意: try_files指令 设置了 文件查找规则 为 $uri $uri/ /index.html。即3个规则,先从 $uri 查找,再从 $uri/ 目录中查找,最后查找 /index.html。
Uncaught SyntaxError: Unexpected token ‘<‘
- 发现:相关报错的
/static/js都指向了同一个html,原因就是使用了try_files $uri $uri/ /index.html - 解决:重定向该地址
location /baseUrl/static {
alias D:/nginx-1.22.0/html/static;
}
注意:地址不能有空格、中文