1.yum安裝依賴
yum -y install gcc gcc-c++ autoconf pcre-devel make automake
yum -y install wget httpd-tools vim #安裝vim 編輯器
2.建立自己的工程目錄
mkdir app
mkdir backup
mkdir download
mkdir heng
mkdir logs
mkdir work
3.查看一下yum是否已經(jīng)存在
yum list | grep nginx
#配置yum源
如果不存在追他,或者不是你需要的版本被因,那我們可以自行配置yum源
vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx.repo
baseurl=http://nginx.org/packages/centos/7/$basearch/ # centos/7指定版本
gpgcheck=0
enabled=1
4.yum安裝Nginx
yum -y install nginx
#檢測Nginx的版本
nginx -v
#查看Nginx的安裝目錄 -q 代表詢問模式咐熙,-l 代表返回列表
rpm -ql nginx
5.nginx.conf 文件解讀
cd /etc/nginx
vim nginx.conf
----------------
#運行用戶赛不,默認(rèn)即是nginx叽掘,可以不進(jìn)行設(shè)置
user? nginx;
#Nginx進(jìn)程拣技,一般設(shè)置為和CPU核數(shù)一樣
worker_processes? 1;?
#錯誤日志存放目錄
error_log? /var/log/nginx/error.log warn;
#進(jìn)程pid存放位置
pid? ? ? ? /var/run/nginx.pid;
events {
? ? worker_connections? 1024; # 單個后臺進(jìn)程的最大并發(fā)數(shù)
}
http {
? ? include? ? ? /etc/nginx/mime.types;? #文件擴(kuò)展名與類型映射表
? ? default_type? application/octet-stream;? #默認(rèn)文件類型
? ? #設(shè)置日志模式
? ? 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? /var/log/nginx/access.log? main;? #nginx訪問日志存放位置
? ? sendfile? ? ? ? on;? #開啟高效傳輸模式
? ? #tcp_nopush? ? on;? ? #減少網(wǎng)絡(luò)報文段的數(shù)量
? ? keepalive_timeout? 65;? #保持連接的時間偏形,也叫超時時間
? ? #gzip? on;? #開啟gzip壓縮
? ? include /etc/nginx/conf.d/*.conf; #包含的子配置項位置和文件
----------------------
# 進(jìn)入conf.d目錄敏储,然后使用vim default.conf進(jìn)行查看。
server {
? ? listen? ? ? 80;? #配置監(jiān)聽端口
? ? server_name? localhost;? //配置域名
? ? #charset koi8-r;? ?
? ? #access_log? /var/log/nginx/host.access.log? main;
? ? location / {
? ? ? ? root? /usr/share/nginx/html;? ? #服務(wù)默認(rèn)啟動目錄
? ? ? ? index? index.html index.htm;? ? #默認(rèn)訪問文件
? ? ? ? #allow? 45.76.202.231;#設(shè)置可以訪問IP ,all 所有IP
? ? ? ? #deny? 123.9.51.42; #設(shè)置禁止訪問IP ,all 所有IP
? ? ? ? #注:指令allow和deny是有先后順序的(也就是誰先觸發(fā)丁侄,誰起作用)
? ? }
? ? #error_page? 404? ? ? ? ? ? ? /404.html;? # 配置404頁面
? ? #error_page? 404 http://xxx.com; #指定訪問網(wǎng)址
? ? # redirect server error pages to the static page /50x.html
? ? #
? ? error_page? 500 502 503 504? /50x.html;? #錯誤狀態(tài)碼的顯示頁面惯雳,配置后需要重啟
? ? location = /50x.html {
? ? ? ? root? /usr/share/nginx/html;
? ? }
? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? #
? ? #location ~ \.php$ {
? ? #? ? proxy_pass? http://127.0.0.1;
? ? #}
? ? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
? ? #
? ? #location ~ \.php$ {
? ? #? ? root? ? ? ? ? html;
? ? #? ? fastcgi_pass? 127.0.0.1:9000;
? ? #? ? fastcgi_index? index.php;
? ? #? ? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
? ? #? ? include? ? ? ? fastcgi_params;
? ? #}
? ? # deny access to .htaccess files, if Apache's document root
? ? # concurs with nginx's one
? ? #
? ? #location ~ /\.ht {
? ? #? ? deny? all;
? ? #}
}
# 服務(wù)目錄放在了/usr/share/nginx/html下
cd /usr/share/nginx/html
ls
默認(rèn)有 50x.html? index.html 兩個文件
6.Nginx服務(wù)啟動、停止鸿摇、重啟
#使用systemctl命令啟動
systemctl start nginx.service
#查詢服務(wù)的運行狀況
ps aux | grep nginx #有三條記錄石景,說明我們Nginx被正常開啟了。
#停止Nginx服務(wù)的四種方法
1.立即停止服務(wù)
nginx? -s stop
2.從容停止服務(wù)
nginx -s quit
3.killall 方法殺死進(jìn)程
killall nginx
4.systemctl 停止
systemctl stop nginx.service
5.重啟Nginx服務(wù)
systemctl restart nginx.service /root/app/html
6.重新載入配置文件(改完配置文件用)
nginx -s reload
注:在默認(rèn)情況下拙吉,Nginx啟動后會監(jiān)聽80端口潮孽,從而提供HTTP訪問,如果80端口已經(jīng)被占用則會啟動失敗筷黔。我么可以使用netstat -tlnp命令查看端口號的占用情況往史。
#關(guān)閉防火墻(頁面訪問不到可能是開啟了防火墻)
-----------
#查看防火墻狀態(tài):
systemctl status firewalld.service
執(zhí)行后可以看到綠色字樣標(biāo)注的“active(running)”,說明防火墻是開啟狀態(tài)
#關(guān)閉運行的防火墻:systemctl stop firewalld.service
#關(guān)閉后必逆,使用命令:systemctl status firewalld.service 查看防火墻狀態(tài)
可以看到怠堪,disavtive(dead)的字樣,說明防火墻已經(jīng)關(guān)閉
# 一旦重啟操作系統(tǒng)名眉,防火墻就自動開啟了粟矿,該怎么設(shè)置才能永久關(guān)閉防火墻呢?
輸入命令:systemctl disable firewalld.service损拢,禁止防火墻服務(wù)器
------------
7.訪問控制權(quán)限匹配
location =/img{
? allow all; #可以訪問
? deny all; #不可以訪問
}
注:= 號代表精確匹配陌粹,使用了=后是根據(jù)其后的模式進(jìn)行精確匹配。
#正則表達(dá)式設(shè)置訪問權(quán)限
location ~\.php$ {
? deny all;
}
注:這樣我們再訪問的時候就不能訪問以php結(jié)尾的文件了福压,~ 后跟正則
8.Nginx設(shè)置虛擬主機(jī)
#配置虛擬主機(jī)可以基于端口號掏秩、基于IP和基于域名,直接配置在主文件里etc/nginx/nginx.conf文件里荆姆, 也可以配置在子配置文件里etc/nginx/conf.d/default.conf蒙幻。我這里為了配置方便,就配置在子文件里了胆筒。當(dāng)然你也可以再新建一個文件邮破,只要在conf.d文件夾下就可以了。
1.etc/nginx/conf.d文件夾下新建 server.conf文件
------
server{
? ? ? ? listen 8001;? #配置監(jiān)聽端口
? ? ? ? server_name localhost; #配置域名
? ? ? ? root /usr/share/nginx/html/html8001; #指向的根目錄
? ? ? ? index index.html; #主頁默認(rèn)打開文件
}
------
8.Nginx使用域名設(shè)置虛擬主機(jī)
#修改etc/nginx/conf.d目錄下的server.conf 文件仆救,把原來的80端口虛擬主機(jī)改為以域名劃分的虛擬主機(jī)抒和。
server{
? ? ? ? listen 8080;
? ? ? ? server_name www.xxxx.com;
? ? ? ? location / {
? ? ? ? ? ? ? ? root /usr/share/nginx/html/html8001; #指向的根目錄
? ? ? ? ? ? ? ? index index.html index.htm; #主頁默認(rèn)打開文件
? ? ? ? }
}
9.Nginx反向代理的設(shè)置
-----------------------
#現(xiàn)在我們要訪問http://axx.com然后反向代理到 bxx.com這個網(wǎng)站 (域名或IP都可以)
server{
? ? ? ? listen 80;
? ? ? ? server_name axx.com;
? ? ? ? location / {
? ? ? ? ? ? ? proxy_pass http://bxx.com;
? ? ? ? }
}
反向代理常用的指令:
proxy_set_header :在將客戶端請求發(fā)送給后端服務(wù)器之前,更改來自客戶端的請求頭信息彤蔽。
proxy_connect_timeout:配置Nginx與后端代理服務(wù)器嘗試建立連接的超時時間摧莽。
proxy_read_timeout : 配置Nginx向后端服務(wù)器組發(fā)出read請求后,等待相應(yīng)的超時時間顿痪。
proxy_send_timeout:配置Nginx向后端服務(wù)器組發(fā)出write請求后镊辕,等待相應(yīng)的超時時間油够。
proxy_redirect :用于修改后端服務(wù)器返回的響應(yīng)頭中的Location和Refresh。
配置跨域代理:
location / {
? add_header 'Access-Control-Allow-Origin' $http_origin;
? add_header 'Access-Control-Allow-Credentials' 'true';
? add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
? add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
? add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
? if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
? }
------------------------
10.Nginx適配PC或移動設(shè)備
#Nginx通過內(nèi)置變量$http_user_agent丑蛤,可以獲取到請求客戶端的userAgent叠聋,就可以用戶目前處于移動端還是PC端撕阎,進(jìn)而展示不同的頁面給用戶受裹。
server{
? listen 80;
? server_name nginx2.jspang.com;
? location / {
? root /usr/share/nginx/pc;
? if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
? ? ? root /usr/share/nginx/mobile;
? }
? index index.html;
? }
}
11.Nginx的Gzip壓縮配置
#gzip是需要服務(wù)器和瀏覽器同事支持的
gzip最簡單的配置
http {
? .....
? ? gzip on;
? ? gzip_types text/plain application/javascript text/css;
? .....
}
gzip模塊指令:
------------
gzip : 該指令用于開啟或 關(guān)閉gzip模塊。
gzip_buffers : 設(shè)置系統(tǒng)獲取幾個單位的緩存用于存儲gzip的壓縮結(jié)果數(shù)據(jù)流虏束。
gzip_comp_level : gzip壓縮比棉饶,壓縮級別是1-9,1的壓縮級別最低镇匀,9的壓縮級別最高照藻。壓縮級別越高壓縮率越大,壓縮時間越長汗侵。
gzip_disable : 可以通過該指令對一些特定的User-Agent不使用壓縮功能幸缕。
gzip_min_length:設(shè)置允許壓縮的頁面最小字節(jié)數(shù),頁面字節(jié)數(shù)從相應(yīng)消息頭的Content-length中進(jìn)行獲取晰韵。
gzip_http_version:識別HTTP協(xié)議版本发乔,其值可以是1.1.或1.0.
gzip_proxied : 用于設(shè)置啟用或禁用從代理服務(wù)器上收到相應(yīng)內(nèi)容gzip壓縮。
gzip_vary : 用于在響應(yīng)消息頭中添加Vary:Accept-Encoding,使代理服務(wù)器根據(jù)請求頭中的Accept-Encoding識別是否啟用gzip壓縮雪猪。