什么是nginx ?
其實(shí)就是一個(gè)輕量級(jí)的服務(wù)器拼坎,可以很好的處理反向代理和負(fù)載均衡盾碗;可以很好的處理靜態(tài)資源;所以很適合我們前端使用邮弹,也很簡單黔衡。
我們主要用來做接口轉(zhuǎn)發(fā)(以及一些其他事情)。
nginx 命令行
-
啟動(dòng)
sudo nginx
-
測試配置文件(也可以用來查看配置文件的路徑)
sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
-
重新加載配置文件
sudo nginx -s reload
-
其他(停止腌乡,退出盟劫,重啟)
sudo nginx -s (stop|quit|reopen)
nginx.config
nginx的關(guān)鍵在于配置文件的設(shè)置,里面參數(shù)很多与纽,可以參見文檔侣签。
這里只介紹我們可能會(huì)用到的塘装。
vim /etc/nginx/nginx.conf
# 設(shè)置用戶組
#user nobody;
# 占用內(nèi)核數(shù),一般設(shè)置為服務(wù)器最高內(nèi)核
worker_processes 1;
# error log 存放位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# 每一個(gè)worker進(jìn)程能并發(fā)處理的最大連接數(shù)
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# 開啟gzip壓縮
gzip on;
# gzip默認(rèn)不壓縮javascript硝岗、圖片等靜態(tài)資源文件
gzip_types text/plain application/x-javascript text/css text/javascript;
#nginx上傳限制,默認(rèn)為1M;
client_max_body_size 6m;
# 引入其他配置文件
include /etc/nginx/conf.d/*.conf;
#### 重點(diǎn)在這個(gè)里面 ####
server {
listen 80;
# 訪問的Domain
server_name 10.142.78.40;
# 根目錄
root E:\work;
# 文件夾索引氢哮,生產(chǎn)環(huán)境要關(guān)閉
autoindex on;
# 匹配到/的時(shí)候默認(rèn)加載index.html 然后在找index.htm
index index.html index.htm;
# 這2行需要加進(jìn)來,不然頁面的中文可能會(huì)出現(xiàn)亂碼
default_type ‘text/html’;
charset utf-8;
# header 允許_字符
underscores_in_headers on;
location ~(/usrcenter){
# 匹配到usrcenter型檀, 轉(zhuǎn)發(fā)到http://10.142.78.40:8787/usrcenter
proxy_pass http://10.142.78.40:8787;
}
location /o2blog_wx/ {
# 當(dāng)訪問xxxx/o2blog_wx的時(shí)候轉(zhuǎn)發(fā)到服務(wù)器上的http://127.0.0.1:3000
# 通過rewrite字段重寫,將o2blog_wx進(jìn)行正則匹配替換
# 也就是xxxx/o2blog_wx/hello =》 http://127.0.0.1:3000/hello
proxy_pass http://127.0.0.1:3000;
rewrite ^/o2blog_wx/(.*) /$1 break;
}
# 不讓dist的東西去匹配/ 里面的內(nèi)容
location ~(/dist){
# 關(guān)閉靜態(tài)資源緩存听盖,方便dbeug胀溺;生產(chǎn)環(huán)境不要用
expires off;
# expires 365d;
}
# 將/下面的URL 都重寫到/index.html
location / {
rewrite ^ /index.html break;
index index.html index.htm;
}
## 設(shè)置302 跳轉(zhuǎn)
location /o2blog_wx/ {
# 當(dāng)匹配到http://aotu.jd.com/o2blog_wx/的時(shí)候會(huì)跳轉(zhuǎn)到http://aotu.jd.com/wxblog
return 302 http://aotu.jd.com/wxblog
}
error_log /var/log/nginx/html_error.log;
# 將404 和50X 重定向到 對應(yīng)的報(bào)錯(cuò)頁面
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
}
nginx 前端的其他用途
https
環(huán)境切換
在nginx里面拿cookie;根據(jù)cookie跳轉(zhuǎn)到不同的環(huán)境接口皆看;很方便的測試接口
set $env_id "1.1.1.1";
if ( $http_cookie~* "host_id=(\S+)(;.*|$)") {
set $env_id $1;
}
location / {
proxy_set_header Host $host;
proxy_pass http://$env_id:80;
}
- 內(nèi)容劫持
nginx_http_footer_filter 是淘寶開發(fā)的一個(gè)nginx模塊仓坞;可以在文件的底部添加文字;比如往html里面添加小廣告等等呵呵~
- CDN combo
利用nginx_http_concat,將請求合并腰吟,通過這樣的方式http://example.com/??style1.css,style2.css,foo/style3.css訪問合并后的資源无埃。(也是阿里系的常用做法)
- 適配PC與移動(dòng)web
通過判斷UA,做302跳轉(zhuǎn)到 pc的路徑和H5的路徑
ps:查看端口是否被占用:
sudo lsof -i :8090