nginx--->是一個軟件,web軟件,提供web服務html
nagios監控用的ios
apache與nginx都是http協議nginx
最總是apache,很大很全很穩定,過重web
針對apache過重的問題,nginx就是輕便裝,apache
nginx在併發上採用了網絡io模型和反向代理很好vim
網絡io:網絡傳輸數據,centos
nginx用epoll模型,使nginx處理速度快,併發作得好(同時處理多個任務)瀏覽器
把CentOS-相關的yum源都移到yum源文件夾中緩存
[root@localhost yum.repos.d]# mv /root/aa/CentOS-* .網絡
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo
[root@localhost yum.repos.d]#
清緩存
[root@localhost yum.repos.d]# yum clean all
安裝nginx的擴展源
[root@localhost yum.repos.d]# yum install epel-release –y
安裝完畢後看到的yum源:
centos-7.repo CentOS-fasttrack.repo epel.repo
CentOS-Base.repo CentOS-Media.repo epel-testing.repo
CentOS-CR.repo CentOS-Sources.repo httpd.repo
CentOS-Debuginfo.repo CentOS-Vault.repo
安裝nginx
yum install nginx -y
查看接收目錄
vim /etc/nginx/nginx.conf
從上面的程序,咱們看到目錄是:
root /usr/share/nginx/html
嘗試執行看看,先建個目錄和文件/usr/share/nginx/html/a/b/c.txt
[root@bogon ~]# mkdir /usr/share/nginx/html/a/b/ -p
[root@bogon ~]# echo 'welecome boy'>/usr/share/nginx/html/a/b/c.txt
要讓上面的生效,就要重啓
[root@bogon ~]# systemctl restart nginx
這裏有個問題:restart是先執行
systemctl stop nginx
再執行
systemctl start nginx可能停了就起不來,網站就掛了。
爲了減小風險,我只是從新加載,reload就行,只讀新內容
看看啓動狀態
[root@bogon ~]# systemctl status nginx
查看個人IP:ifconfig
192.168.88.128:
在瀏覽器輸入http://192.168.88.128:80/a/b/c.txt
發現鏈接不上,404頁面
關防火牆
systemctl stop firewalld
再次鏈接,有結果:
原理是
Nginx會訪問它的配置文件,找到根目錄
nginx----------------ànginx.conf----------------à/usr/share/nginx/html/a/b/c.txt------走http協議------看到內容
若是開機也不想開防火牆,則
systemctl disable firewalld
開機啓動防火牆
systemctl enable firewalld
開機啓動nginx
systemctl enable nginx
操做防火牆(是否內核防火牆,公司每每有硬件防火牆)
Iptables
Uri地址是惟一的,url是百度,uri是我經過百度而後下載到本地,文件經過http協議下載到我這裏
http://202.102.10.10:80/a/b/c.txt
http://-------->server端是基於http協議給client發數據的
202.102.10.10:80--->定位到全世界範圍內惟一一款軟件是誰
/a/b/c.txt--->/usr/share/nginx/html/a/b/c.txt(軟件的根)
補充:
location是url地址、/是統一位置在哪裏,若是不寫的話,就默認/usr/share/nginx/html;,也能夠寫一個目錄
root /var/www/html;
location / { root /var/www/html
}
修改完重啓:
systemctl restart nginx
這裏有個問題:restart是先執行
systemctl stop nginx
再執行
systemctl start nginx可能停了就起不來,網站就掛了。
爲了減小風險,我只是從新加載,reload就行,只讀新內容
[root@localhost ~]# mkdir -p /var/www/html/
[root@localhost ~]# echo '123453434342323'> /var/www/html/index.html
在訪問以前的路徑就404
由於默認路徑已經修改成/var/www/html/
http://192.168.88.128/index.html
若是我把/index.html去掉,發現也能夠訪問
location是url地址、/是統一位置在哪裏,若是不寫的話,就默認/usr/share/nginx/html;,也能夠寫一個目錄
root /var/www/html;
/是若是root /var/www/html不帶後面的東西,就默認index.html,若是沒有index.html,就訪問index.htm,若是也沒有,就訪問index.a
location / { root /var/www/html
index index.html index.htm index.a;
}
網站每每都默認打開首頁:
index.html
我嘗試默認該爲a.txt,以下:
[root@localhost ~]# vim /etc/nginx/nginx.conf
location / { root /var/www/html
index a.txt index.html index.htm index.a;
}
建立文件a.txt
[root@localhost ~]# touch /var/www/html/a.txt
[root@localhost ~]# echo 'I love you' > /var/www/html/a.txt
[root@localhost ~]# systemctl reload nginx
訪問記錄
tail -f /var/log/nginx/access.log
192.168.88.1 - - [18/Nov/2017:13:50:04 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-"
客戶端寫的日誌
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
個人IP
192.168.88.1='$remote_addr
-=-
-=$remote_user
[18/Nov/2017:13:50:04 +0800]= [$time_local]
"$request"是請求:"GET / HTTP/1.1" http1.1是最新的協議
$status=304,若是200是訪問成功
$body_bytes_sent 0個字節,14是14個字節
$http_referer ="-"http協議,referer能夠以前上一個頁面,若是一個教學資源須要在看了廣告後才能下載,而一些人盜鏈了,不用廣告也能夠下載,那referer就是他的頁面,咱們發現若是referer不是咱們的頁面的話,就要先打開咱們的頁面,才能下載。
$http_user_agent:個人瀏覽器
$http_x_forwarded_for
能夠本身定義日誌格式:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format aaa '$remote_addr - [$time_local] "$request"';
access_log /var/log/nginx/access.log aaa;