雲伺服器+nginx配置實戰
雙十一了,阿里雲推出了史上最優惠的雲伺服器產品(點選這裡檢視詳情),1核2G新使用者只需要99元/年,一次性買3年只需要不到300元,價格確實極大的優惠。

要是老使用者的話可以用新開一個賬號,用親戚朋友的身份證認證一下也能享受1折優惠,加入我的戰隊(點選這裡檢視詳),邀請一個人差不多能瓜分戰隊的 50元分紅+50元現金紅包+25%返現
。打個比方, 假如你是新使用者,你買了一個3年的雲伺服器一共300塊,買過後,你成功邀請一人(下單3年的雲伺服器)就可分50(戰隊紅包) + 50(現金紅包) + 300*25%(返利紅包)。相當於買伺服器的錢回來了一半(折上5折)
,聽著是不是很誘惑人,進來了解下吧;目前戰隊排名是top15,後名次靠前的話還有更大的優惠。 ofollow,noindex">m.aliyun.com/act/team111…

nginx配置
連線伺服器
很多前端的小夥伴買了雲伺服器後,擔心不會配置,畢竟很多的前端還不是很會配置nginx,這裡小編詳細講解下我買的阿里雲伺服器配置的過程。
點選上面連結就可參團購買,購買過程中推薦系統選擇 CentOS
和 Ubuntu
系統。Ubuntu系統使用者佔用率高,所以文件自然也多,比較適合新手;CentOS比較適合企業和商用,一般看你們公司用的都是CentOS系統的; 具體2這的區別可檢視這裡 小編買的是 Ubuntu 16.04 64位
。其他的選項選擇預設就是,地區只要是國內的都很快。
買好後,點選頭像=>選擇產品服務=>雲伺服器,即可看到購買過的伺服器產品。基本資訊,配置資訊,對cpu的監控一目瞭然。買阿里雲的一個主要原因還是阿里雲盾安全。

可以在 更多
裡面修改遠端連線密碼和重置伺服器密碼,修改好後,嘗試遠端連線下,先輸入遠端連線密碼,後登陸使用者名稱(root)、密碼即可。
當然,也可直接使用命令列連結 :
sudo ssh 伺服器外網ip 輸入電腦密碼,輸入伺服器密碼 複製程式碼

除此之外,你也可用工具連結,對於 lunix
命令不是很熟悉的小夥伴比較實用。 這裡推薦用 Transmit
和 Filezilla
其他的都一樣,使用者名稱輸入 root
,埠選擇 22
即可。
安裝Nginx
更新ubuntu軟體源
sudo apt-get update sudo apt-get install -y python-software-properties software-properties-common sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update 複製程式碼
安裝nginx
sudo apt-get install nginx # 檢查是否安裝成功 nginx -v 複製程式碼
安裝好的檔案位置:
/usr/sbin/nginx:主程式 /etc/nginx:存放配置檔案 /usr/share/nginx:存放靜態檔案 /var/log/nginx:存放日誌 複製程式碼
其實從上面的根目錄資料夾可以知道,Linux系統的配置檔案一般放在 /etc
,日誌一般放在 /var/log
,執行的程式一般放在 /usr/sbin
或者 /usr/bin
。當然,如果要更清楚Nginx的配置項放在什麼地方,可以開啟 /etc/nginx/nginx.conf
。
安裝其他必要工具
安裝nodejs
sudo apt-get install nodejs sudo apt install nodejs-legacy sudo apt install npm 複製程式碼
更新npm的包映象源,方便快速下載
sudo npm config set registry https://registry.npm.taobao.org sudo npm config list 複製程式碼
全域性安裝n管理器(用於管理nodejs版本)
sudo npm install n -g 複製程式碼
安裝最新的nodejs(stable版本)
sudo n stable # 檢查是否安裝成功 node -v npm -v 複製程式碼
安裝webpack
npm install webpack-cli -g npm install webpack -g 複製程式碼
還有的工具後續補上
配置nginx
nginx安裝完成後,配置檔案為 /etc/nginx/nginx.conf
和 /etc/nginx/conf.d/default.conf
cd /etc/nginx vim nginx.conf cd /etc/nginx/conf.d vim default.conf 複製程式碼
自定義nginx.conf 配置
#執行使用者,預設即是nginx,可以不進行設定 user root; #Nginx程序,一般設定為和CPU核數一樣 worker_processes 1; #程序pid存放位置 pid /run/nginx.pid; events { worker_connections1024; # 單個後臺程序的最大併發數 # multi_accept on; } http { ## # Basic Settings ## sendfile on; #開啟高效傳輸模式 tcp_nopush on; #減少網路報文段的數量 tcp_nodelay on; keepalive_timeout 65; #保持連線的時間,也叫超時時間 types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; #副檔名與型別對映表 default_type application/octet-stream; #預設檔案型別 ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; #開啟gzip壓縮 gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; #包含的子配置項位置和檔案 # include /etc/nginx/sites-enabled/*; } #mail { ## See sample authentication script at: ## http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # ## auth_http localhost/auth.php; ## pop3_capabilities "TOP" "USER"; ## imap_capabilities "IMAP4rev1" "UIDPLUS"; # #server { #listenlocalhost:110; #protocolpop3; #proxyon; #} # #server { #listenlocalhost:143; #protocolimap; #proxyon; #} #} 複製程式碼
自定義default.conf 配置
server { listen80;#配置監聽埠 server_namelocalhost;#配置域名 #charset koi8-r; #access_log/var/log/nginx/host.access.logmain; location / { #服務預設啟動目錄 root /usr/share/nginx/html/pc;#pc # nginx適配pc和app裝置 if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') { root /usr/share/nginx/html/app;#app } indexindex.html;#預設訪問檔案 allowall; #允許訪問的ip # denyall;#拒絕訪問的ip } #redirecterror pages to the static page /404.html error_page404/static/html/404/404.html;# 配置404頁面 # redirect server error pages to the static page /50x.html error_page500 502 503 504/static/html/404/500.html;#錯誤狀態碼的顯示頁面,配置後需要重啟 # ^~ 表示uri以某個常規字串開頭,大多情況下用來匹配url路徑,nginx不對url做編碼,因此請求為/static/20%/aa, # 可以被規則^~ /static/ /aa匹配到(注意是空格)。 location ^~ /static/ { root /usr/share/nginx/html; allowall; #允許訪問的ip # denyall;#拒絕訪問的ip } location = /50x.html { root /usr/share/nginx/html/pc/;#pc # nginx適配pc和app裝置 if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') { root /usr/share/nginx/html/app/;#app } } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { #proxy_passhttp://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #roothtml; #fastcgi_pass127.0.0.1:9000; #fastcgi_indexindex.php; #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; #includefastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { #denyall; #} } 複製程式碼
自定義預設目錄
我們服務的預設目錄放在 /usr/share/nginx/html
了下。
cd /usr/share/nginx/html ls 複製程式碼
假如你要定義伺服器的預設訪問目錄,修改 location /
中的root即可,不過需要開通下你自定義目錄的許可權。
以 /root/www
是自定義目錄為例
# 需要一層層分別開通許可權 chmod -R 777 /root chmod -R 777 /root/www 複製程式碼
假如你使用的是 Transmit
等伺服器工具,也可用工具檢視

自定義錯誤頁面
# redirecterror pages to the static page /404.html error_page404/static/html/404/404.html;# 配置404頁面 # redirect server error pages to the static page /50x.html error_page500 502 503 504/static/html/404/500.html;#錯誤狀態碼的顯示頁面,配置後需要重啟 複製程式碼
然後在你預設或者自定義的伺服器目錄下,根據你的錯誤頁配置,新建相關的頁面即可。
Nginx訪問許可權和路徑匹配規則
在匹配規則裡面,有2個欄位可以控制這個規則下的訪問許可權
location / { allowall; #允許訪問的ip # denyall;#拒絕訪問的ip } 複製程式碼
實際情況中,訪問許可權的控制還是比較複雜的,例如,要求伺服器 static
(靜態目錄)所有使用者都能訪問,且,重新定義訪問路徑,我們需要location塊來完成相關的需求匹配。
# ^~ 表示uri以某個常規字串開頭,大多情況下用來匹配url路徑,nginx不對url做編碼,因此請求為/static/20%/aa, # 可以被規則^~ /static/ /aa匹配到(注意是空格)。 location ^~ /static/ { root /usr/share/nginx/html; allowall; #允許訪問的ip # denyall;#拒絕訪問的ip } 複製程式碼
對於nginx路徑匹配規則,也需要簡單的瞭解一下
- = 表示精確匹配
- ^~ 表示uri以某個常規字串開頭,大多情況下用來匹配url路徑,nginx不對url做編碼,因此請求為/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。
- ~ 正則匹配(區分大小寫)
- ~* 正則匹配(不區分大小寫)
- !~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配 的正則
- / 任何請求都會匹配
符號的優先順序
首先匹配 =,其次匹配^~, 其次是按檔案中順序的正則匹配,最後是交給 / 通用匹配。當有匹配成功時候,停止匹配,按當前匹配規則處理請求。
Nginx反向代理的設定
後續補充。
Nginx適配PC或移動裝置
如上述配置:
#服務預設啟動目錄 root /usr/share/nginx/html/pc;#pc # nginx適配pc和app裝置 if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') { root /usr/share/nginx/html/app;#app } 複製程式碼
#服務預設啟動目錄 root /usr/share/nginx/html/pc;#pc # nginx適配pc和app裝置 if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') { root /usr/share/nginx/html/app;#app } 複製程式碼
可用app和pc訪問我的伺服器檢視效果47.99.212.100
配置阿里雲安全組
從上面的配置檔案可以看出,nginx服務監聽的是80埠,所以記得到ECS例項一下開啟埠。步驟如下:
- 進入阿里雲控制檯,並找到ECS例項。
- 點選例項後邊的"更多"
- 點選"網路和安全組" ,再點選"安全組配置"
- 選擇"安全組列表",再點選"安全組配置",再點選"加入安全組規則"
- 進行80埠的設定,具體設定如圖。

nginx 相關命令
在真個伺服器做nginx配置的過程中,我們 ①先安裝nginx;
安裝好後,檢查是否安裝成功; ②然後開始做相關nginx配置
; ③配置完後檢查配置是否正常
; ④然後啟動nginx
; ⑤後續每次改動nginx配置都需重啟nginx
;
啟動nginx 服務
#方法一 nginx #方法二 systemctl start nginx.service 複製程式碼
檢視所有啟動的nginx程序
ps aux | grep nginx 複製程式碼
停止Nginx服務
#方法一 nginx-s stop #方法二 nginx -s quit #方法三 killall nginx 複製程式碼
檢查nginx配置是否正常
nginx -t 複製程式碼
重啟nginx服務
sudo nginx -s reload 複製程式碼
檢視端口占用情況
netstat -tlnp 複製程式碼