当前位置: 首页 > news >正文 网站正在建设中 色系统优化方法 news 2025/10/22 4:47:02 网站正在建设中 色,系统优化方法,手机发布 wordpress文章,如何做网站后台本文将指导您如何配置Nginx以代理前后端分离的项目#xff0c;并特别说明了对WebSocket的代理设置。通过本教程#xff0c;您将能够实现一次性配置#xff0c;进而使项目能够在任意局域网服务器上部署#xff0c;并可通过IP地址或域名访问服务。 笔者建议 先速览本文了解大…本文将指导您如何配置Nginx以代理前后端分离的项目并特别说明了对WebSocket的代理设置。通过本教程您将能够实现一次性配置进而使项目能够在任意局域网服务器上部署并可通过IP地址或域名访问服务。 笔者建议 先速览本文了解大致内容后再复制对应的代码。本文环境基于Windows VUE3 typescript 理论通用于linux 前提条件 已安装Nginx 前后端项目代码部署在服务器上 Nginx配置步骤 基础代理配置 编辑Nginx配置文件通常位于/etc/nginx/nginx.conf或/etc/nginx/conf.d/目录下的某个.conf文件。例如 server {listen 80; # 监听端口server_name localhost; # 服务器名称 局域网内一般就是localhost location / {proxy_pass http://前端项目地址; # 前端项目代理proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;} location /api/ {proxy_pass http://后端项目地址; # 后端接口代理proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;} } 为了自动化区分生产环境和开发环境我们需要使用到“.env” 如图通过.env.development .env.production 然后修改 package.json即可自动区分环境。然后再请求代码中这样写 export const BASE_URL import.meta.env.VITE_APP_BASE_API 以下为笔者实例配置 #user nobody; worker_processes 2;#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;pid logs/nginx.pid;events {worker_connections 2048; }http {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;#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 8119;server_name localhost;# 指定根目录为 dist 文件夹root ./dist;index index.html;location / {try_files $uri $uri/ /index.html;}# 将以/api开始的请求代理到本机的8112端口的后端服务并且去掉URL中的/apilocation /api/ {proxy_pass http://localhost:8112/; # 注意这里的尾部斜线proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 其他可能需要的代理设置...}# 添加通用 WebSocket 代理location /ws/ {proxy_pass http://localhost:8112;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection Upgrade;proxy_set_header Host $host;proxy_read_timeout 60s;proxy_send_timeout 60s;}}server {listen 80;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 Apaches document root# concurs with nginxs 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;# }#}}其中前端的dist目录笔者使用的是相对路径也就意味着/conf/nginx.conf中./ 相对的根目录是conf的同级目录。这也是实现通用化部署的关键。这样配置在其他地方解压后仍然可以生效。 而后端则是全部以 /api 作为默认的请求的url然后通过nginx反向代理到真实的后端。 例如请求的是 /api/login 到服务器 则被代理后为 http://localhost:8112/login 即指向了服务器自身。 WebSocket代理配置 在配置文件中增加针对WebSocket的代理设定。这段代码除了服务地址和指定的代理的路径“/ws/”其余部分是固定配置。 # 添加通用 WebSocket 代理location /ws/ {proxy_pass http://localhost:8112;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection Upgrade;proxy_set_header Host $host;proxy_read_timeout 60s;proxy_send_timeout 60s;} 而websocket则需要再代码中这样写 socket new WebSocket(ws://${window.location.host}/ws/upgrade_log); 此行代码用于创建一个新的WebSocket连接。具体解释如下 ws://${window.location.host}/ws/upgrade_log这是一个模板字符串用来定义WebSocket服务器的URL。 ws://是WebSocket协议的前缀表示这是一个WebSocket连接。在安全环境中例如使用HTTPS时会使用wss://作为前缀。 ${window.location.host}这部分代码使用了JavaScript的模板字符串语法来嵌入一个表达式。window.location.host获取当前浏览器窗口中显示的文档的主机名即域名或IP地址加端口号。这意味着WebSocket连接到当前页面所在的域名或IP地址上。 /ws/upgrade_log这是WebSocket服务在服务器上的具体路径。在Nginx配置中我们可能设置了一个location块来监听/ws/路径并将其代理到实际运行WebSocket服务的后端服务器。 所以整行代码的作用是在客户端创建一个WebSocket连接连接到与当前网页相同host的服务器上特定的/ws/upgrade_log路径。当这个WebSocket连接建立后就可以用于双向通信客户端和服务器可以互相发送消息。 然后通过Nginx的代理实现任何通过ip访问前端网页时都能连接到服务器的websocket服务。 文章转载自: http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.cfmrb.cn.gov.cn.cfmrb.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.rrrrsr.com.gov.cn.rrrrsr.com http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.rstrc.cn.gov.cn.rstrc.cn http://www.morning.pwdgy.cn.gov.cn.pwdgy.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.hzqjgas.com.gov.cn.hzqjgas.com http://www.morning.kpxnz.cn.gov.cn.kpxnz.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.iqcge.com.gov.cn.iqcge.com http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.hsflq.cn.gov.cn.hsflq.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.kbdjn.cn.gov.cn.kbdjn.cn http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn http://www.morning.hpggl.cn.gov.cn.hpggl.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.lxwjx.cn.gov.cn.lxwjx.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.kpxnz.cn.gov.cn.kpxnz.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.mcjp.cn.gov.cn.mcjp.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.fgxr.cn.gov.cn.fgxr.cn 查看全文 http://www.tj-hxxt.cn/news/238757.html 相关文章: 网站建立连接不安全怎么解决这么做介绍网站的ppt 网站开发分工织梦的手机端网站模板下载地址 做毕业设计网站的问题与展望深圳seo优化公司 网站二级页面做哪些东西在线课堂手机网站模板 滕州市 网站建设公司开发一个卖东西的网站多少 企业网站建设中在方案设计上cms免费开源 做徽章的网站深圳龙华新区住房和建设局网站 金乡网站建设哪家好秀网站 域名做好了怎么做网站内容seo课堂 求个网站你懂我的意思吗宣武做网站 做自媒体视频搬运网站销售和营销的区别 iis 网站属性wordpress编辑器功能增强 微网站欣赏无锡网站建设网页制作 搭建一个app平台需要多少钱seo实战密码读后感 wordpress搭建博客简书重庆网站优化公司哪家便宜 可以做很多个网站然后哭推广青岛市黄岛区网站建设 湘西建设监理协会网站萧山城区建设有限公司网站 天津做企业网站公司wordpress 为什么很慢 建站公司做的网站侵权了中国建设银行在哪里 运营商查浏览网站东营网站建设价钱表 海口网站公司抖音引流推广一个30元 discuz 做家教网站京东网站建设流程和结构图 企业网站开发的背景和意义免费相册制作app 庐江网站建设绍兴企业网站建设 哈尔滨做网站seo的表情包生成器在线制作网站 临沂网站优化哪家好包装公司logo设计 网站都有备案号吗网站js效果 WordPress建站要花钱软件项目流程八个阶段 做一个公司的网站应做哪些准备工作公司产品营销广告宣传 佛山外贸网站建设机构手机网站制作架构