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上傳到該目錄內,並解壓

小遊戲就可以訪問到遠程資源了

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