Nginx 配置详解 | 宝塔面板配置静态资源服务器 配置小游戏远程资源服务器

获取更多笔记和源码

公众号:CocosCreator笔记

 

导读

 

 

安装宝塔

 

安装 Nginx

 

 

 

配置 Nginx

 

配置 H5 游戏

 

配置小游戏远程资源

 

1准备云服务器

腾讯云:

https://url.cn/53X4QsE

 

阿里云:

https://www.aliyun.com/sale-season/2020/procurement-new-members?userCode=pbkdohi4

 

2安装宝塔面板

如果是新购买的服务器:

更多 -> 重装系统 -> 服务市场 -> 管理与监控 -> 搜索"宝塔" -> 选择"宝塔Linux面板" -> 开始重装

 

如果是已经安装过操作系统(centos),则使用脚本安装宝塔面板

  •  
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install.sh && sh install.sh

 

安装完后打开:http://ip:8888

输入用户名和密码

 

3安装 Nginx

在软件商店安装 Nginx :

 

安装成功后首页展示:

 

4配置修改

点击首页的Nginx:

 

user 配置为 root ,否则会报 403 的错误

配置 server

  • listen 80
    监听80端口(http)

  • listen 443 ssl
    监听443端口(https)

  • root  /root/workspace/home
     资源根目录,默认访问目录中的index.html

  • ssl_certificate ssl_certificate_key
    ssl证书存放路径

  • expires 30d
     缓存时效30天

  • add_header Access-Control-Allow-Origin *
    允许跨域

  • proxy_pass http://127.0.0.1:7000
    端口代理(可用于nodejs)

 

更改完后,点击保存,并重载配置:

 

完整的配置文件(也可在公众号回复:nginx.conf):

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
user root;  #配置用户或者组worker_processes auto;  #允许生成的进程数error_log /www/wwwlogs/nginx_error.log crit;  #指定日志路径,级别pid /www/server/nginx/logs/nginx.pid; #指定Nginx进程运行文件存放地址worker_rlimit_nofile 51200; #配置Nginx进程最大打开文件数
events {  use epoll;  worker_connections 51200; #最大连接数  multi_accept on;}
http {  include mime.types;    include proxy.conf;    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 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 {    listen 80;  #监听端口(http)    listen 443 ssl; #监听端口(https)    server_name localhost;  #监听地址(127.0.0.1)    root /root/workspace/home;  #根目录        ssl_certificate /root/workspace/ssl/xxx.crt;    ssl_certificate_key /root/workspace/ssl/xxx.key;
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {        #expires 30d;    }
    location ~ .*\.(js|css)?$ {        #expires 12h;    }        location / {          add_header Access-Control-Allow-Origin *;        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';      }    }      server {    #自定义端口    listen 6000 ssl;    server_name localhost;        ssl_certificate /root/workspace/ssl/xxx.crt;    ssl_certificate_key /root/workspace/ssl/xxx.key;      location / {      root /root/workspace/test;    }  }    server {    #代理nodejs端口    listen 7000 ssl;    server_name localhost;        ssl_certificate /root/workspace/ssl/xxx.crt;    ssl_certificate_key /root/workspace/ssl/xxx.key;      location / {      proxy_pass http://127.0.0.1:8000;    }  }      include /www/server/panel/vhost/nginx/*.conf;}

 

  • gzip on
    on为启用,off为关闭

  • gzip_min_length 1k
    设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。

  • gzip_buffers 4 16k
    获取多少内存用于缓存压缩结果,‘4 16k’表示以16k*4为单位获得

  • gzip_comp_level 5
    gzip压缩比(1~9),越小压缩效果越差,但是越大处理越慢,所以一般取中间值;

  • gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php
    对特定的MIME类型生效,其中'text/html’被系统强制启用

  • gzip_http_version 1.1
    识别http协议的版本,早期浏览器可能不支持gzip自解压,用户会看到乱码

  • gzip_vary on
    启用应答头"Vary: Accept-Encoding"

  • gzip_proxied off
    nginx做为反向代理时启用,off(关闭所有代理结果的数据的压缩),expired(启用压缩,如果header头中包括"Expires"头信息),no-cache(启用压缩,header头中包含"Cache-Control:no-cache"),no-store(启用压缩,header头中包含"Cache-Control:no-store"),private(启用压缩,header头中包含"Cache-Control:private"),no_last_modefied(启用压缩,header头中不包含"Last-Modified"),no_etag(启用压缩,如果header头中不包含"Etag"头信息),auth(启用压缩,如果header头中包含"Authorization"头信息)

  • gzip_disable msie6
    (IE5.5和IE6 SP1使用msie6参数来禁止gzip压缩 )指定哪些不需要gzip压缩的浏览器(将和User-Agents进行匹配),依赖于PCRE库

 

5
配置H5游戏

游戏发布为 WebMobile,将构建后的文件打包,上传到之前配置的路径:root /root/workspace/home

通过https://ip 或者http://ip可以直接访问到index.html

 

6配置小游戏远程资源

游戏发布为微信小游戏,在构建面板中,填写远程服务器地址:https://ip/1.0.0/

构建后,将 res 目录打包

 

在home目录下新建版本号目录:1.0.0,然后将zip上传到该目录内,并解压

小游戏就可以访问到远程资源了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章