nginx使用

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;
    }
}
  1. location匹配首先是根據最長路徑優先匹配原則,比如http://localhost/images/example.png, 這時候nginx首先把請求轉向/data/images/example.png, 如果沒有則返回404頁面。
  2. 當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

五、參考文檔

官方文檔地址:http://nginx.org/en/docs/beginners_guide.html

發佈了172 篇原創文章 · 獲贊 21 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章