nginx 配置多个 server

nginx 配置多个 server

前段时间部署我的个人博客的时候 博客前端部署的端口号是 80,当时不是很了解 nginx 怎么配置多个 server 的 就把之前代理数据库面板的 888 端口改成了 80,这下博客是能访问 但是我的数据库面板 888 端口代理不到了 打开数据库面板就是 404 昨天迁移博客 又遇到了这个问题 于是就研究了一下如何写多个 server

配置如下: 主要看 server 其他的配置是 nginx 自带的 不过我知道的都会打上注释的 可以了解一下

user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

stream {
  log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';

  access_log /www/wwwlogs/tcp-access.log tcp_format;
  error_log /www/wwwlogs/tcp-error.log;
  include /www/server/panel/vhost/nginx/tcp/*.conf;
}

events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
  include mime.types;
  #include luawaf.conf;

  include proxy.conf;
  lua_package_path "/www/server/nginx/lib/lua/?.lua;;";

  default_type application/octet-stream;

  server_names_hash_bucket_size 512;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 50m;

  sendfile on;
  tcp_nopush on;

  keepalive_timeout 60;

  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 256k;
  fastcgi_intercept_errors on;
  # 开启gzip压缩 gzip一般是用于前端项目的代码压缩 压缩后代码可以小 10 倍左右 压缩后的项目加载速度杠杠的 必须打包代码时使用gzip压缩 并且nginx里配置了才会生效
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
  gzip_vary on;
  gzip_proxied expired no-cache no-store private auth;
  gzip_disable "MSIE [1-6]\.";

  limit_conn_zone $binary_remote_addr zone=perip:10m;
  limit_conn_zone $server_name zone=perserver:10m;

  server_tokens off;
  access_log off;

  # 数据库面板 server
  server {
    listen 888; // 监听 888 端口
    server_name phpmyadmin;
    index index.html index.htm index.php; // 找到面板html
    root /www/server/phpmyadmin; // 在哪里去找
    location ~ /tmp/ {
      return 403;
    }

    #error_page   404   /404.html;
    include enable-php.conf;

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
      expires 30d;
    }

    location ~ .*\.(js|css)?$ {
      expires 12h;
    }

    location ~ /\. {
      deny all;
    }

    access_log /www/wwwlogs/access.log;
  }
  # 前端服务server 其实server监听不同的端口都是一样的 只不过 80 端口是默认端口 在浏览器输入地址不需要输入 80 端口就可以访问到服务
  server {
    listen 80;
    server_name localhost;
    # 这里教大家如何代理前端服务
    location / {
      root /www/wwwroot/xxxxx/xxxx/dist; // 比如我打包的前端项目地址文件放在这里 注意 只有一个前端时 使用 root 多个的时候 第一个用root 其他的用 alias
      index index.html index.htm; // 这里配置入口文件
    }
    # 这是其他的前端服务 配置会和第一个配置不同
    location /other {
      alias /www/wwwroot/blog/blogV3/admin/dist; // 需要用 alias 指定文件目录
      index index.html index.htm;
    }
    # 这里是静态文件访问 比如我们可以通过 ip + /emoji/happy.png 访问这个文件夹下的制定图片
    location /emoji {
      alias /www/wwwroot/emoji;
      autoindex on; // 开启静态服务
    }
    # 后端服务代理
    location /api/ {
      proxy_pass http://服务器ip:8080/; # 服务端代理地址 或者是域名(域名解析以后要能指向服务器ip)
      proxy_set_header Host $host; # 获取用户真实ip
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    # 其他服务端代理
    location /gitee/ {
      proxy_pass https://xxx.com/;
    }
  }
  include /www/server/panel/vhost/nginx/*.conf;
}

不知道怎么在vite里开启gzip的可以去看看这篇文章vite 开启 gzip 压缩

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值