本文主要是介绍nginx反向代理实现二级域名转一级域名,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 需求场景:
- 有两个大项目交互必须在一级域名下,每个项目有多个前端,之前采用二级或三级域名的方式导致域名过多不好维护,故由project1.service1.city.com、project1.service2.city.com、project1.service3.city.com的方式,转换为project1.city.com/service1/、project1.city.com/service2/、project1.city.com/service3/
后续域名仅需按项目维护 - 单个前端服务均为docker方式,使用nginx将静态资源转发,docker -p将端口映射出来
- 实现方式
采用nginx反向代理,注意项目内路径的设置问题,本次遇见的问题均有路由错误导致,供参考
# 项目nginx配置文件如下server
{listen 80;listen 443 ssl http2;server_name eztest.city.com;index index.php index.html index.htm default.php default.htm default.html;root /www/wwwroot/eztest.city.com;#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则#error_page 404/404.html;add_header Strict-Transport-Security "max-age=31536000";error_page 497 https://$host$request_uri;#PHP-INFO-START PHP引用配置,可以注释或修改include enable-php-00.conf;#PHP-INFO-END#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效include /www/server/panel/vhost/rewrite/eztest.city.com;#REWRITE-END#禁止访问的文件或目录location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md){return 404;}#一键申请SSL证书验证目录相关设置location ~ \.well-known{allow all;}# ez-adminlocation /admin/ {proxy_pass http://192.168.10.217:7813/;}location /toolbox/ {proxy_pass http://192.168.10.217:7802/;}# entrancelocation / {proxy_pass http://192.168.10.217:7809/;}location /manager/ {proxy_pass http://192.168.10.217:7808/;}location /report/ {proxy_pass http://192.168.10.217:7815/;}location /scene/ {proxy_pass http://192.168.10.217:7806/;}access_log /www/wwwlogs/eztest.city.com.log access_json;error_log /www/wwwlogs/eztest.city.com.error.log;
}
备注:
- vue项目注意vue.config.js中publicPath:’./’,的值,若有跟路径/,可能在多次转发后找不到,建议配置为当前路径
- 第二种前端项目有两层,若出现200状态,页面空白的现象,需找前端人员协同查看,本次修改router.js文件,强制转换路由
服务nginx配置文件参考如下:
server {listen 8080 default_server;listen [::]:8080 default_server;server_name nginx_vue_front;gzip on;gzip_min_length 1k;gzip_comp_level 9;gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css application/font-sfnt;gzip_disable "MSIE [1-6]\.";gzip_vary on;root /www/wwwroot/dist/;location / {try_files $uri $uri/ /index.html;}location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {access_log off;add_header Cache-Control max-age=360000;}
}
dockerfile参考如下:
/www/wwwroot # cat Dockerfile
FROM node:12.18.0-alpine3.11ARG NPM_RUN_ARG=build
RUN apk add nginx tzdata
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeADD . /www/wwwroot/
WORKDIR /www/wwwroot/
RUN yarn config set registry https://registry.npm.taobao.org/ && yarn install && yarn cache clean
RUN npm run --silent $NPM_RUN_ARG
RUN ln -s /www/wwwroot/deploy/nginx.conf /etc/nginx/conf.d/
RUN rm -f /etc/nginx/mime.types; ln -s /www/wwwroot/deploy/mime.types /etc/nginx/CMD ["/usr/sbin/nginx", "-g", "pid /tmp/nginx.pid; daemon off;"]
这篇关于nginx反向代理实现二级域名转一级域名的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!