官方安裝方法
https://www.nginx.com/resources/wiki/start/topics/tutorials/install/
centos7 安裝方法
touch /etc/yum.repos.d/nginx.repo
vi nginx.repo
添加如下信息
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
sudo yum install nginx
nginx -v
啟動(dòng)關(guān)閉重啟
sudo systemctl start nginx
sudo systemctl stop nginx
systemctl restart nginx.service
systemctl enable nginx.service
systemctl disable nginx.service
systemctl status nginx.service
配置信息
語(yǔ)法規(guī)則:
location [=|~|~*|^~] /uri/ {
…
}
= 開(kāi)頭表示精確匹配
^~ 開(kāi)頭表示uri以某個(gè)常規(guī)字符串開(kāi)頭疚顷,理解為匹配 url路徑即可
~ 開(kāi)頭表示區(qū)分大小寫(xiě)的正則匹配
~* 開(kāi)頭表示不區(qū)分大小寫(xiě)的正則匹配
!~和!~* 分別為區(qū)分大小寫(xiě)不匹配及不區(qū)分大小寫(xiě)不匹配的正則
/ 通用匹配,任何請(qǐng)求都會(huì)匹配到凿歼。
匹配規(guī)則與順序
首先匹配 =,
其次匹配^~班挖,
其次是按文件中順序的正則匹配现使,
最后是交給 / 通用匹配。
當(dāng)有匹配成功時(shí)候漫拭,停止匹配,按當(dāng)前匹配規(guī)則處理請(qǐng)求混稽。
例子采驻,有如下匹配規(guī)則:
location = / {#規(guī)則A}
location = /login {#規(guī)則B}
location ^~ /static/ {#規(guī)則C}
location ~ \.(gif|jpg|png|js|css)$ {#規(guī)則D}
location ~* \.png$ {#規(guī)則E}
location !~ \.xhtml$ {#規(guī)則F}
location !~* \.xhtml$ {#規(guī)則G}
location / {#規(guī)則H}
那么產(chǎn)生的效果如下:
http://localhost/ 將匹配規(guī)則A
http://localhost/login 將匹配規(guī)則B
http://localhost/register 則匹配規(guī)則H
http://localhost/static/a.html 將匹配規(guī)則C
http://localhost/a.gif 將匹配規(guī)則D和規(guī)則E,但是規(guī)則D順序優(yōu)先匈勋,規(guī)則E不起作用
http://localhost/static/c.png 則優(yōu)先匹配到規(guī)則C
http://localhost/a.PNG 則匹配規(guī)則E礼旅,而不會(huì)匹配規(guī)則D,因?yàn)橐?guī)則E不區(qū)分大小寫(xiě)颓影。
幾個(gè)常用規(guī)則
location = / {
proxy_pass http://tomcat:8080/index
}
location ^~ /static/ {
root /webroot/static/
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/
}
location / {
proxy_pass http://tomcat:8080/
}
```
> 一些可用的全局變量
```
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
```