Nginx介紹和使用

Nginx介紹和使用

1、介紹

Nginx是一個十分輕量級而且高性能HTTP和反向代理服務器,一樣也是一個IMAP/POP3/SMTP代理服務器。html

2、特性

  • HTTP服務器
  • 反向代理服務器
  • 簡單的負載均衡和容錯
  • 支持熱部署

3、nginx模塊

nginx模塊通常分爲三類:nginx

  • handler:負責處理客戶端請求併產生待響應內容。
  • filter:負責對輸出的內容進行處理,能夠對輸出進行修改。
  • upstream:實現反向代理功能,將真正的請求轉發到後端服務器上,並從後端服務器上讀取響應,發回客戶端。upstream模塊是一種特殊的handler,只不過響應內容不是真正由本身產生的,而是從後端服務器上讀取的。

4、ubuntu安裝nginx

sudo apt-get install nginx

這種方式安裝的文件位置:web

/usr/sbin/nginx:主程序shell

/etc/nginx:存放配置文件ubuntu

/usr/share/nginx:存放靜態文件segmentfault

/var/log/nginx:存放日誌後端

經過這種方式安裝的,會自動建立服務,會自動在/etc/init.d/nginx新建服務腳本,而後就可使用sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}的命令啓動。瀏覽器

能夠再/var/log/nginx/下查看日誌,若是端口80被佔用,就更改/etc/nginx/sites-enabled/default文件,將下面的兩個80改爲你想要的的端口,而後從新啓動。tomcat

19 
 20 server {
 21         listen 80 default_server;
 22         listen [::]:80 default_server ipv6only=on;

5、配置nginx做爲http服務器

1. 配置項目的conf文件

/etc/nginx/nginx.conf中能夠看到自定義配置文件的路徑:服務器

71         include /etc/nginx/conf.d/*.conf;
 72         include /etc/nginx/sites-enabled/*; 
 73 }

在conf.d目錄下新建文件timeline.conf,寫入配置內容

# HTTP Server

server {
    listen   8080;
    server_name  bonnenuit.vip www.bonnenuit.vip;

    location / {
        alias /home/wangjun/tomcat8/webapps/timeline/pages/;
        index index.html;
    }

}

重啓nginxsudo service nginx restart

2. 經過瀏覽器訪問

http://bonnenuit.vip:8080/ 若是顯示正常,則說明配置成功。

3. 遇到的問題

1. 報錯"server" directive is not allowed here in /etc/nginx/myconf/timeline.conf:3

出現這個錯誤的緣由是include /etc/nginx/xxx/*.conf;沒有寫在http標籤下,由於server只能出如今http下面。

2. 訪問url的時候報錯403 forbidden

查詢/var/log/nginx/error.log,具體的報錯日誌爲:

2019/07/03 10:21:25 [error] 1523#0: *1 open() "/home/wangjun/tomcat8/webapps/timeline/pages/timeline/pages/index.html" failed (13: Permission denied), client: 106.39.75.134, server: bonnenuit.vip, request: "GET /timeline/pages/index.html HTTP/1.1", host: "bonnenuit.vip:8080"

出現這個緣由是由於nginx的worker進程沒法訪問靜態資源文件,由於worker進程的用戶和資源的全部者是不同的,咱們須要更改配置文件nginx.conf:

# user 用戶 用戶組
user wangjun wangjun;

而後重啓nginx就能夠解決。

6、配置nginx做爲反向代理服務器

1. 配置conf文件

在conf.d目錄下timeline.conf中,新增配置內容:

# HTTP Server

# 反向代理服務器+負載均衡
upstream test_reverse_proxy {
        server 120.25.245.241:8080 weight=1 max_fails=2 fail_timeout=10s;
        server 120.25.245.241:8080 weight=1 max_fails=2 fail_timeout=10s; #兩臺機器能夠作負載均衡,目前只有一臺機器,所以ip:port同樣,一臺模擬兩臺
        keepalive 16;
}


server {
        listen   8080;
        server_name  bonnenuit.vip www.bonnenuit.vip; #server_name是爲了區別多個server時,匹配域名來決定進入哪一個server,當都不匹配時,進入配置的第一個server

        location / {
                alias /home/wangjun/tomcat8/webapps/timeline/pages/;
                index index.html;
        }

        location ^~ /proxy/ {
                proxy_set_header Host $host;
                proxy_pass http://test_reverse_proxy/;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
                proxy_redirect off;
                proxy_intercept_errors on;
                client_max_body_size 10m;
        }

}

2. 經過瀏覽器訪問

http://bonnenuit.vip:8080/proxy/ 若是顯示正常,則說明配置成功。

7、location匹配url規則

看下location的語法:

location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }

location 後面跟可選的修飾符,後面就是要匹配的字符,花括號是對應的配置。

修飾符含義:

= 表示精確匹配,只有請求的url路徑與後面的字符串徹底相等時,纔會命中。
~ 表示該規則是使用正則定義的,區分大小寫
~* 表示該規則是使用正則定義的,不區分大小寫
^~ 表示若是該符號後面的字符是最佳匹配,採用該規則,再也不進行後續的查找

具體的匹配過程以下:

  • 首先先檢查使用前綴字符定義的location,選擇最長匹配的項並記錄下來。
  • 若是找到了精確匹配的location,也就是使用了=修飾符的location,結束查找,使用它的配置。
  • 而後按順序查找使用正則定義的location,若是匹配則中止查找,使用它定義的配置。
  • 若是沒有匹配的正則location,則使用前面記錄的最長匹配前綴字符location。

基於以上的匹配過程,咱們能夠獲得如下兩點啓示:

  1. 使用正則定義的location在配置文件中出現的順序很重要。由於找到第一個匹配的正則後,查找就中止了,後面定義的正則就是再匹配也沒有機會了。
  2. 使用精確匹配能夠提升查找的速度。例如常常請求/的話,可使用=來定義location。
參考:

https://www.cnblogs.com/Eason...

https://www.w3cschool.cn/ngin...

https://segmentfault.com/a/11...

相關文章
相關標籤/搜索