一胁澳、Nginx簡(jiǎn)介
1.Nginx是什么
Nginx是一款輕量級(jí)Web服務(wù)器、也是一款反向代理服務(wù)器
2.Nginx能干什么
①可直接支持Rails和PHP的程序
②可作為HTTP反向代理服務(wù)器
③作為負(fù)載均衡服務(wù)器
④作為郵件代理服務(wù)器
⑤幫助實(shí)現(xiàn)前端動(dòng)靜分離
3.Nginx特點(diǎn)
- 高穩(wěn)定
- 高性能
- 資源占用少
- 功能豐富
- 模塊化結(jié)構(gòu)
- 支持熱部署
二森篷、Nginx安裝
1.下載:http://nginx.org/download/nginx-1.10.2.zip
2.解壓縮
3.運(yùn)行nginx.exe:通過(guò)雙擊圖標(biāo)或者cmd命令行運(yùn)行
三式镐、Nginx常用命令
1.測(cè)試配置文件
安裝路徑下的 nginx.exe -t
2.啟動(dòng)命令
安裝路徑下的 nginx.exe
3.停止命令
安裝路徑下的 nginx.exe -s stop,
或者是:nginx.exe -s quit
4.重啟命令
安裝路徑下的 nginx.exe -s reload
5.查看進(jìn)程命令
ps -ef |grep nginx
6.平滑重啟
kill -HUP 【Nginx主進(jìn)程號(hào)(即查看進(jìn)程命令查到的PID)】
7.增加防火墻訪(fǎng)問(wèn)權(quán)限
①sudo vim /etc/sysconfig/iptables
②-A INPUT -p tcp -m state --state NEW
-m tcp --dport 80 -j ACCEPT
③保存退出
④重啟防火墻 sudo service iptables restart
四陶耍、Nginx虛擬域名配置及測(cè)試驗(yàn)證
配置步驟:
1.編輯sudo vim /usr/local/nginx/conf/nginx.conf
①增加include vhost/*.conf;
②保存退出
2.在/usr/local/nginx/conf/目錄新建vhost文件夾:
即:/user/local/nginx/conf/vhost
3.創(chuàng)建域名轉(zhuǎn)發(fā)配置文件
image.hcxjingdong.com.conf:轉(zhuǎn)向目錄的反向代理:
server {
listen 80;
autoindex off;
server_name image.hcxjingdong.com;
access_log c: /access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /
404. html;
if ($query_string~ * ".*[\;'\<\>].*") {
return 404;
}
location~/(mmall_fe|mmall_admin_fe)/dist / view /* {
deny all;
}
location / {
root C:\ftpfile\img;
add_header Access-Control-Allow-Origin *;
}
}
tomcat.hcxjingdong.com.conf:轉(zhuǎn)向端口的反向代理:
server {
listen 80;
autoindex on;
server_name tomcat.hcxjingdong.com;
access_log c: /access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /
404. html;
if ($query_string~ * ".*[\;'\<\>].*") {
return 404;
}
location / {
proxy_pass http: //127.0.0.1:8080;
add_header Access-Control-Allow-Origin *;
}
}
4.啟動(dòng)(重啟)驗(yàn)證
①啟動(dòng):${nginx}/sbin/nginx
②重啟:${nginx}/sbin/nginx -s reload
注:${nginx}代表安裝在系統(tǒng)中的路徑玉凯,例如:/usr/local/nginx
5.訪(fǎng)問(wèn)驗(yàn)證
使用默認(rèn)80端口訪(fǎng)問(wèn)驗(yàn)證:http://localhost:80或http://127.0.0.1:80
6.指向端口
http轉(zhuǎn)發(fā)
server{
listen 80;
autoindex off;
server_name learning.hcxjingdong.com;
access_log c:/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location / {
proxy_pass http://127.0.0.1:81/learning;
add_header Access-Control-Allow-Origin *;
}
}
listen 80:監(jiān)聽(tīng)80端口势腮;
autoindex off:是否創(chuàng)建首頁(yè)的索引目錄;
當(dāng)nginx接到image.hcxjingdong.com(二級(jí)域名)請(qǐng)求漫仆,就轉(zhuǎn)發(fā)到:http://127.0.0.1:81/learning目錄下
7.指向目錄
線(xiàn)上圖片服務(wù)器烙如,為前端提供的前端部署服務(wù)器都是通過(guò)指向目錄的反向代理
server{
listen 80;
autoindex off;
server_name img.hcxjingdong.com;
access_log c:/access.log combined;
index index.html index.htm index.jsp index.php;
#root /product/front/;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location ~ /(hcxjingdong_fe|hcxmall_admin_fe)/dist/view/* {
deny all;
}
location / {
root \product\ftpfile\img;
add_header Access-Control-Allow-Origin *;
}
}
root /product/ftpfile/img:
root直接指向硬盤(pán)系統(tǒng)目錄product文件夾下的ftpfile下的img文件夾死陆;
即在訪(fǎng)問(wèn)img.hcxjingdong.com的時(shí)候就直接指向了該文件夾
8.測(cè)試驗(yàn)證
五呀非、Nginx注意事項(xiàng)
可以配置域名轉(zhuǎn)發(fā)晋涣,但請(qǐng)一定要配置host,并且使host生效之后才可以狸眼,設(shè)置完成之后要重啟瀏覽器
Windows下配置:
①進(jìn)入c:\Windows\System32\drivers\etc
②用記事本打開(kāi)hosts文件
③添加好對(duì)應(yīng)的域名及ip
④保存退出
例如:
10.211.55.6 image.hcx.com
10.211.55.6 s.hcx.com
六、配置Windows下的Nginx
配置hosts:
c:\Windows\System32\drivers\etc
用瀏覽器訪(fǎng)問(wèn)www.hcxjingdong.com
包括本機(jī)訪(fǎng)問(wèn)http://localhost:
配置目錄的轉(zhuǎn)發(fā)
1.進(jìn)入到nginx.conf(nginx的主配置):
添加:include vhost/*.conf;
2.按照該路徑去創(chuàng)建此文件夾:
在conf文件夾下創(chuàng)建vhost文件夾
3.在vhost文件夾中創(chuàng)建文件:image.hcxjingdong.com.conf
文件內(nèi)容:
server{
listen 80;
autoindex off;
server_name image.hcxjingdong.com;
access_log c:/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location ~ /(hcxmall_fe|hcxmall_admin_fe)/dist/view/* {
deny all;
}
location / {
root C:\ftpfile\img;
add_header Access-Control-Allow-Origin *;
}
}
到C:\ftpfile\img目錄下存放圖片以便訪(fǎng)問(wèn)
4.修改本機(jī)的host浴滴,讓本機(jī)的nginx配合到image.hcxjingdong.com域名
去到C:\Windows\System32\drivers\etc目錄下修改hosts文件:
5.重啟nginx:
進(jìn)入到nginx目錄執(zhí)行命令:
①nginx.exe -t:驗(yàn)證配置文件是否正確
②nginx.exe -s reload:重啟nginx
6.訪(fǎng)問(wèn)域名(image.hcxjingdong.com)驗(yàn)證圖片是否生效:
測(cè)試host是否生效:image.hcxjingdong.com
測(cè)試圖片是否生效:http://image.hcxjingdong.com/hcx.jpg
配置ip端口的轉(zhuǎn)發(fā)
1.在conf下的vhost下創(chuàng)建:tomcat.hcxjingdong.com.conf
使用tomcat域名進(jìn)行ip端口轉(zhuǎn)發(fā)拓萌,轉(zhuǎn)發(fā)到tomcat服務(wù)上
tomcat.hcxjingdong.com.conf:
server{
listen 80;
autoindex off;
server_name tomcat.hcxjingdong.com;
access_log c:/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location / {
proxy_pass http://127.0.0.1:8080;
add_header Access-Control-Allow-Origin *;
}
}
2.配置hosts:
3.啟動(dòng)tomcat
4.重啟nginx:nginx.exe -s reload
5.訪(fǎng)問(wèn)http://tomcat.hcxjingdong.com
成功顯示tomcat啟動(dòng)頁(yè),說(shuō)明http的轉(zhuǎn)發(fā)也成功了