官方安装方法
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
启动关闭重启
sudo systemctl start nginx
sudo systemctl stop nginx
systemctl restart nginx.service
systemctl enable nginx.service
systemctl disable nginx.service
systemctl status nginx.service
配置信息
语法规则:
location [=|~|~*|^~] /uri/ {
…
}
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!~和!~* 分别为区分大小写不匹配及不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到。
匹配规则与顺序
首先匹配 =,
其次匹配^~,
其次是按文件中顺序的正则匹配,
最后是交给 / 通用匹配。
当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
例子,有如下匹配规则:
location = / {#规则A}
location = /login {#规则B}
location ^~ /static/ {#规则C}
location ~ \.(gif|jpg|png|js|css)$ {#规则D}
location ~* \.png$ {#规则E}
location !~ \.xhtml$ {#规则F}
location !~* \.xhtml$ {#规则G}
location / {#规则H}
那么产生的效果如下:
http://localhost/ 将匹配规则A
http://localhost/login 将匹配规则B
http://localhost/register 则匹配规则H
http://localhost/static/a.html 将匹配规则C
http://localhost/a.gif 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用
http://localhost/static/c.png 则优先匹配到规则C
http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
几个常用规则
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
```
网友评论