騰訊雲服務nginx部署靜態項目


一直想要搭建自己的blog,買了基礎雲服務器練手

文章內容是根據騰訊文檔(https://cloud.tencent.com/document/product/213/2131)總結

部署靜態頁面歸納為2點:①:項目相關的文件上傳到雲服務上;②:可以通過IP或者域名打開頁面

一:首先需要一台騰訊雲服務器

學生優惠https://cloud.tencent.com/act/campus,也可以活動時購買,我用的是linux系統;

二:安裝winSCP/FileZilla,查看文件以及上傳文件

我用的是winSCP,在騰訊官網下載安裝就好了,輸入雲服務的密碼,ip就可以登錄---》可以在雲服務登錄上查看,密碼忘記了也可以重置;

winSCP與FileZilla使用方法類似,連接成功之后,可以本地文件和雲服務文件相互傳輸;

三:安裝Nginx

在 CentOS 上,可直接使用 yum 來安裝 Nginx

yum install nginx -y

安裝完成后,使用 nginx 命令啟動 Nginx:

nginx

此時,訪問 http://<您的域名> 可以看到 Nginx 的測試頁面

四:配置靜態服務器訪問路徑

外網用戶訪問服務器的 Web 服務由 Nginx 提供,Nginx 需要配置靜態資源的路徑信息才能通過 url 正確訪問到服務器上的靜態資源。

打開 Nginx 的默認配置文件 /etc/nginx/nginx.conf ,修改 Nginx 配置,將默認的 root /usr/share/nginx/html; 修改為: root /data/www;,如下:

示例代碼:/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root /data/www;

        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

現在我們需要重啟 Nginx 讓新的配置生效,如:

nginx -s reload

在根目錄下,新建data/www文件夾:

mkdir -p /data/www

可以通過winSCP上傳一個index.html文件到data/www文件下,通過ip/index.html或者域名/index.html就可以打開index.html頁面;

一個基於 Nginx 的靜態服務器就搭建完成了,現在所有放在 /data/www 目錄下的的靜態資源都可以直接通過域名訪問。

 

complete!

 

雲服務器可以配置域名,這樣就可以通過域名來訪問頁面,但是需要備案。

文章只是顯示index.html頁面,如果是基於node項目,只要運行相關的app.js文件就好了,但是是無法訪問后台數據的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM