download下來(lái)侣签,解壓到e:/盤(pán),修改配置文件,E:\nginx\conf下的nginx.conf文件
常用命令
在安裝目錄下钞速,打開(kāi)cmd命令行 (別的命令行工具不行)
start nginx //啟動(dòng)nginx
nginx -s stop //關(guān)閉
nginx -s reload //更改配置文件后吵取,用這個(gè)命令重啟nginx
切勿重復(fù)點(diǎn)擊nginx.exe啟動(dòng) 容易出現(xiàn)錯(cuò)誤刑赶,反復(fù)修改配置文件報(bào)500,試著重裝nginx試試
修改配置文件酥郭,啟動(dòng)靜態(tài)服務(wù)华坦,這里我是在 E:\somepage\dist目錄下放了一個(gè)spa應(yīng)用,配置完啟動(dòng)正常
如果要自定義域名不从,需要修改host文件映射
http {
server {
listen 80; //監(jiān)聽(tīng)的端口
server_name localhost; //啟動(dòng)的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { // 根路由代理的目錄惜姐,這里是 E:\somepage\dist目錄,默認(rèn)打開(kāi)index.html文件
root E:\somepage\dist;
index index.html index.htm;
}
}
server{
listen 8000; //修改端口為8000打開(kāi)頁(yè)面
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
一次500報(bào)錯(cuò) ,下面配置啟動(dòng)ng會(huì)報(bào)錯(cuò)500
server {
listen 8000;
server_name localhost;
location / {
root E:\ngProject\vueDemo;
index index.html index.htm;
}
}
查看error.log (在logs目錄下)
2019/07/02 09:58:31 [crit] 128#7016: *58 CreateFile() "E:
gProject\vueDemo/favicon.ico" failed (123: The filename, directory name, or volume label syntax is incorrect), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8000", referrer: "http://localhost:8000/vueDemo/"
解決 :查看日志發(fā)現(xiàn)是路徑有問(wèn)題椿息,E:后面有一個(gè)換行符歹袁,猜測(cè)是\n被當(dāng)成了換行符 ,修改root: root E:\\ngProject\vueDemo;
nginx重定向的2種方式撵颊,
server {
listen 8000;
server_name localhost;
#server級(jí)別的 root配置 宇攻,相當(dāng)于配置了兜底的location / {...}
root E:\\ngProject\vueDemo;
index index.html index.htm;
location /proxy1/ {
# proxy_pass 指令后面跟上url代理轉(zhuǎn)發(fā)
proxy_pass http://www.baidu.com/;
}
location /proxy2/ {
#rewrite 是實(shí)現(xiàn)URL重定向的重要指令,語(yǔ)法: rewrite regex replacement[flag];
rewrite ^/(.*) http://www.baidu.com permanent;
}
}
proxy_pass 指令注意點(diǎn):后面跟的url 末尾帶/和不帶/是有區(qū)別的
例如:nginx請(qǐng)求鏈接:http://localhost:8000/api/getName?name=xiaoming
a.第一種情況:proxy_pass鏈接的最后不帶"/"
server {
listen 8000;
server_name localhost;
location ^~ /api/ {
proxy_pass http://localhost:8082;
}
}
代理結(jié)果:http://localhost:8082/api/getName?name=xiaoming
b.第二種情況:proxy_pass鏈接的最后帶"/"
server {
listen 8000;
server_name localhost;
location ^~ /api/ {
proxy_pass http://localhost:8082/;
}
}
代理結(jié)果:http://localhost:8082/getName?name=xiaoming
root 和alias指令的區(qū)別 :
root的處理結(jié)果是:root路徑+location路徑 ;alias的處理結(jié)果是:使用alias路徑替換location路徑
root 實(shí)例:
location ^~ /t/ {
root /www/root/html/;
}
如果一個(gè)請(qǐng)求的URI是/t/a.html時(shí)倡勇,web服務(wù)器將會(huì)返回服務(wù)器上的/www/root/html/t/a.html的文件逞刷。
alias實(shí)例:
location ^~ /t/ {
alias /www/root/html/new_t/;
}
如果一個(gè)請(qǐng)求的URI是/t/a.html時(shí),web服務(wù)器將會(huì)返回服務(wù)器上的/www/root/html/new_t/a.html的文件妻熊。注意這里是new_t夸浅,因?yàn)閍lias會(huì)把location后面配置的路徑丟棄掉,把當(dāng)前匹配到的目錄指向到指定的目錄扔役。
注意:
- 使用alias時(shí)帆喇,目錄名后面一定要加"/"。
- alias在使用正則匹配時(shí)亿胸,必須捕捉要匹配的內(nèi)容并在指定的內(nèi)容處使用坯钦。
- alias只能位于location塊中。(root可以不放在location中)
vue history模式ngxin配置:
# vue history模式,代碼在目錄html/dist里侈玄, nginx后臺(tái)配置:
# 訪問(wèn): url http://localhost/{routerPath}
location / {
root html/dist;
index index.html index.htm;
#這里會(huì)根據(jù)匹配的路勁去找文件婉刀,找不到的時(shí)候,會(huì)返回html/dist/index.html文件序仙,
# vueRouter會(huì)根據(jù){routerPath}展示對(duì)應(yīng)的路由頁(yè)面
try_files $uri $uri/ /index.html;
}
# 訪問(wèn): url http://localhost/dist/{routerPath} 突颊,同時(shí)vue打包配置需更改pubilcPath:'/dist',
#這樣項(xiàng)目里面的資源都會(huì)被轉(zhuǎn)發(fā)到這里
#注意:::還需要 修改router配置為:{path:/dist ,children:[]},路由全部掛到/dist下律秃, '/'根路由重定向到/dist
location ^~ /dist {
alias html/dist/;
index index.html index.htm;
try_files $uri $uri/ /dist/index.html; #注意這里是 /dist/index.html
}