nginx使用
一、nginx安装
详见:http://blog.csdn.net/zhengyong15984285623/article/details/53871752
一、nginx配置信息
查看命令nginx -V
,结果如下所示:
nginx version: nginx/1.10.1
built by clang 7.0.2 (clang-700.1.81)
built with OpenSSL 1.0.2h 3 May 2016
TLS SNI support enabled
configure arguments: --prefix=/usr/local/Cellar/nginx/1.10.1 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/usr/local/Cellar/nginx/1.10.1/bin/nginx --with-cc-opt='-I/usr/local/Cellar/pcre/8.38/include -I/usr/local/Cellar/openssl/1.0.2h_1/include' --with-ld-opt='-L/usr/local/Cellar/pcre/8.38/lib -L/usr/local/Cellar/openssl/1.0.2h_1/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_gzip_static_module
二、nginx常用命令
命令 | 备注 |
---|---|
nginx | 启动命令 |
nginx -s stop | 强制迅速关闭nginx进程 |
nginx -s quit | 优雅关闭nginx进程 |
nginx -s reload | 重新加载配置文件 |
nginx -s reopen | 重新打开log文件 |
nginx -t | 测试并检测nginx.conf语法 |
nginx -v | 输出nginx版本 |
nginx -V | 输出nginx版本和配置信息 |
nginx -c file | 使用指定的nginx.conf配置文件 |
三、location说明
安装默认location
位置是/usr/local/Cellar/nginx/1.10.1/
server {
# 1.根路径
location / {
root /data/www;
}
# 2.路径前缀(相对路径,相对/usr/local/Cellar/nginx/1.10.1)
location /images/ {
root /data;
}
# 3.别名路径(绝对路径),与2(路径前缀)一样
location /alias/images/ {
alias /usr/local/Cellar/nginx/1.10.1/data/images/;
}
# 4.反向代理
location / {
proxy_pass http://localhost:8080/;
}
# 5.正则
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
location
匹配首先是根据最长路径优先匹配原则,比如http://localhost/images/example.png
, 这时候nginx首先把请求转向/data/images/example.png
, 如果没有则返回404页面。- 当nginx选择一个
location
来提供请求时,它首先检查指定前缀的位置指令,记住具有最长前缀的location
,然后检查正则表达式。 如果存在与正则表达式的匹配,nginx选择此location
,否则,它选择先前记住的位置。
四、nginx系统日志
不同操作系统日志可能出现位置不同,比如下面两种情况,mac为第一种:
# log location 1
/usr/local/var/log/nginx/access.log
/usr/local/var/log/nginx/error.log
# log location 2
/var/log/nginx/access.log
/var/log/nginx/error.log