ToB企服应用市场:ToB评测及商务社交产业平台

标题: 前端怎样使用Nginx署理dist网页,署理websocket,署理后端 [打印本页]

作者: 王柳    时间: 2024-12-26 12:14
标题: 前端怎样使用Nginx署理dist网页,署理websocket,署理后端
本文将引导您怎样配置Nginx以署理前后端分离的项目,并特殊说明确对WebSocket的署理设置。通过本教程,您将可以或许实现一次性配置,进而使项目可以或许在任意局域网服务器上摆设,并可通过IP地址或域名访问服务。
笔者建议 先速览本文相识大致内容后再复制对应的代码。本文情况基于Windows VUE3 typescript 理论通用于linux
前提条件


Nginx配置步骤

基础署理配置
编辑Nginx配置文件,通常位于/etc/nginx/nginx.conf或/etc/nginx/conf.d/目次下的某个.conf文件。例如:
  1. server {
  2.    listen       80; # 监听端口
  3.    server_name  localhost; # 服务器名称 局域网内一般就是localhost
  4.    location / {
  5.        proxy_pass http://前端项目地址; # 前端项目代理
  6.        proxy_set_header Host $host;
  7.        proxy_set_header X-Real-IP $remote_addr;
  8.        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9.    }
  10.    location /api/ {
  11.        proxy_pass http://后端项目地址; # 后端接口代理
  12.        proxy_set_header Host $host;
  13.        proxy_set_header X-Real-IP $remote_addr;
  14.        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  15.    }
  16. }
复制代码
为了主动化区分生产情况和开发情况,我们必要使用到“.env”

如图通过.env.development      .env.production  然后修改 package.json
即可主动区分情况。然后再请求代码中这样写

  1. export const BASE_URL = import.meta.env.VITE_APP_BASE_API
复制代码
以下为笔者实例配置
  1. #user  nobody;
  2. worker_processes  2;
  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;
  6. pid        logs/nginx.pid;
  7. events {
  8.     worker_connections  2048;
  9. }
  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;
  13.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14.     #                  '$status $body_bytes_sent "$http_referer" '
  15.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  16.     #access_log  logs/access.log  main;
  17.     sendfile        on;
  18.     #tcp_nopush     on;
  19.     #keepalive_timeout  0;
  20.     keepalive_timeout  65;
  21.     #gzip  on;
  22.     server {
  23.     listen 8119;
  24.     server_name localhost;
  25.     # 指定根目录为 dist 文件夹
  26.     root ./dist;
  27.     index index.html;
  28.     location / {
  29.         try_files $uri $uri/ /index.html;
  30.     }
  31.     # 将以/api开始的请求代理到本机的8112端口的后端服务,并且去掉URL中的/api
  32.     location /api/ {
  33.         proxy_pass http://localhost:8112/; # 注意这里的尾部斜线
  34.         proxy_set_header Host $host;
  35.         proxy_set_header X-Real-IP $remote_addr;
  36.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  37.         proxy_set_header X-Forwarded-Proto $scheme;
  38.         # 其他可能需要的代理设置...
  39.     }
  40.     # 添加通用 WebSocket 代理
  41.     location /ws/ {
  42.         proxy_pass http://localhost:8112;
  43.         proxy_http_version 1.1;
  44.         proxy_set_header Upgrade $http_upgrade;
  45.         proxy_set_header Connection "Upgrade";
  46.         proxy_set_header Host $host;
  47.         proxy_read_timeout 60s;
  48.         proxy_send_timeout 60s;
  49.         
  50.     }
  51.     }
  52.     server {
  53.         listen       80;
  54.         server_name  localhost;
  55.         #charset koi8-r;
  56.         #access_log  logs/host.access.log  main;
  57.         location / {
  58.             root   html;
  59.             index  index.html index.htm;
  60.         }
  61.         #error_page  404              /404.html;
  62.         # redirect server error pages to the static page /50x.html
  63.         #
  64.         error_page   500 502 503 504  /50x.html;
  65.         location = /50x.html {
  66.             root   html;
  67.         }
  68.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  69.         #
  70.         #location ~ \.php$ {
  71.         #    proxy_pass   http://127.0.0.1;
  72.         #}
  73.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  74.         #
  75.         #location ~ \.php$ {
  76.         #    root           html;
  77.         #    fastcgi_pass   127.0.0.1:9000;
  78.         #    fastcgi_index  index.php;
  79.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  80.         #    include        fastcgi_params;
  81.         #}
  82.         # deny access to .htaccess files, if Apache's document root
  83.         # concurs with nginx's one
  84.         #
  85.         #location ~ /\.ht {
  86.         #    deny  all;
  87.         #}
  88.     }
  89.     # another virtual host using mix of IP-, name-, and port-based configuration
  90.     #
  91.     #server {
  92.     #    listen       8000;
  93.     #    listen       somename:8080;
  94.     #    server_name  somename  alias  another.alias;
  95.     #    location / {
  96.     #        root   html;
  97.     #        index  index.html index.htm;
  98.     #    }
  99.     #}
  100.     # HTTPS server
  101.     #
  102.     #server {
  103.     #    listen       443 ssl;
  104.     #    server_name  localhost;
  105.     #    ssl_certificate      cert.pem;
  106.     #    ssl_certificate_key  cert.key;
  107.     #    ssl_session_cache    shared:SSL:1m;
  108.     #    ssl_session_timeout  5m;
  109.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  110.     #    ssl_prefer_server_ciphers  on;
  111.     #    location / {
  112.     #        root   html;
  113.     #        index  index.html index.htm;
  114.     #    }
  115.     #}
  116. }
复制代码
其中前端的dist目次笔者使用的是相对路径,也就意味着/conf/nginx.conf中./ 相对的根目次是conf的同级目次。这也是实现通用化摆设的关键。这样配置在其他地方解压后仍然可以见效。

而后端则是全部以 /api 作为默认的请求的url,然后通过nginx反向署理到真实的后端。
例如请求的是 /api/login 到服务器 则被署理后为 http://localhost:8112/login  即指向了服务器自身。

WebSocket署理配置
在配置文件中增加针对WebSocket的署理设定。这段代码除了服务地址和指定的署理的路径“/ws/”别的部分是固定配置。
  1.     # 添加通用 WebSocket 代理
  2.     location /ws/ {
  3.         proxy_pass http://localhost:8112;
  4.         proxy_http_version 1.1;
  5.         proxy_set_header Upgrade $http_upgrade;
  6.         proxy_set_header Connection "Upgrade";
  7.         proxy_set_header Host $host;
  8.         proxy_read_timeout 60s;
  9.         proxy_send_timeout 60s;
  10.         
  11.     }
复制代码

而websocket则必要再代码中这样写

  1. socket = new WebSocket(`ws://${window.location.host}/ws/upgrade_log`);
复制代码
此行代码用于创建一个新的WebSocket连接。具体解释如下:

所以,整行代码的作用是,在客户端创建一个WebSocket连接,连接到与当前网页类似host的服务器上,特定的/ws/upgrade_log路径。当这个WebSocket连接建立后,就可以用于双向通信,客户端和服务器可以相互发送消息。
然后通过Nginx的署理实现任何通过ip访问前端网页时都能连接到服务器的websocket服务。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4