Nginx (engine x) 是一個輕量級高性能的HTTP和反向代理服務器,同時也是一個通用 代理服務器 (TCP/UDP/IMAP/POP3/SMTP),最初由俄羅斯人Igor Sysoev編寫。
基本命令
nginx -t 檢查配置文件是否有語法錯誤
nginx -s reload 熱加載,重新加載配置文件
nginx -s stop 快速關閉
nginx -s quit 等待工作進程處理完成后關閉
搭建好nginx服務器并啟動過后,我們先看nginx默認配置,再逐個介紹不同使用場景。
默認配置
Nginx 安裝目錄下, 我們復制一份`nginx.conf`成`nginx.conf.default`作為配置文件備份,然后修改`nginx.conf`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# 工作進程的數量 worker_processes 1; events { worker_connections 1024; # 每個工作進程連接數 } http { include mime.types; default_type application /octet-stream ; # 日志格式 log_format access '$remote_addr - $remote_user [$time_local] $host "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$clientip"' ; access_log /srv/log/nginx/access .log access; # 日志輸出目錄 gzip on; sendfile on; # 鏈接超時時間,自動斷開 keepalive_timeout 60; # 虛擬主機 server { listen 8080; server_name localhost; # 瀏覽器訪問域名 charset utf-8; access_log logs /localhost .access.log access; # 路由 location / { root www; # 訪問根目錄 index index.html index.htm; # 入口文件 } } # 引入其他的配置文件 include servers/*; } |
搭建站點
在其他配置文件`servers`目錄下,添加新建站點配置文件 xx.conf。
電腦 hosts 文件添加 127.0.0.1 xx_domian# 虛擬主機
1
2
3
4
5
6
7
8
9
10
11
12
13
|
server { listen 8080; server_name xx_domian; # 瀏覽器訪問域名 charset utf-8; access_log logs /xx_domian .access.log access; # 路由 location / { root www; # 訪問根目錄 index index.html index.htm; # 入口文件 } } |
執行命令 nginx -s reload,成功后瀏覽器訪問 xx_domian 就能看到你的頁面
根據文件類型設置過期時間
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
location ~.*\.css$ { expires 1d; break ; } location ~.*\.js$ { expires 1d; break ; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { access_log off; expires 15d; #保存15天 break ; } # curl -x127.0.0.1:80 http://www.test.com/static/image/common/logo.png -I #測試圖片的max-age |
禁止文件緩存
開發環境經常改動代碼,由于瀏覽器緩存需要強制刷新才能看到效果。這是我們可以禁止瀏覽器緩存提高效率
1
2
3
|
location ~* \.(js|css|png|jpg|gif)$ { add_header Cache-Control no-store; } |
防盜鏈
可以防止文件被其他網站調用
1
2
3
4
5
6
7
|
location ~* \.(gif|jpg|png)$ { # 只允許 192.168.0.1 請求資源 valid_referers none blocked 192.168.0.1; if ($invalid_referer) { rewrite ^/ http: // $host /logo .png; } } |
靜態文件壓縮
1
2
3
4
5
6
7
8
9
10
11
12
|
server { # 開啟gzip 壓縮 gzip on; # 設置gzip所需的http協議最低版本 (HTTP/1.1, HTTP/1.0) gzip_http_version 1.1; # 設置壓縮級別,壓縮級別越高壓縮時間越長 (1-9) gzip_comp_level 4; # 設置壓縮的最小字節數, 頁面Content-Length獲取 gzip_min_length 1000; # 設置壓縮文件的類型 (text/html) gzip_types text /plain application /javascript text /css ; } |
執行命令 nginx -s reload,成功后瀏覽器訪問
指定定錯誤頁面
1
2
3
4
5
|
# 根據狀態碼,返回對于的錯誤頁面 error_page 500 502 503 504 /50x .html; location = /50x .html { root /source/error_page ; } |
執行命令 nginx -s reload,成功后瀏覽器訪問
跨域問題
跨域的定義
同源策略限制了從同一個源加載的文檔或腳本如何與來自另一個源的資源進行交互。這是一個用于隔離潛在惡意文件的重要安全機制。通常不允許不同源間的讀操作。
同源的定義
如果兩個頁面的協議,端口(如果有指定)和域名都相同,則兩個頁面具有相同的源。nginx解決跨域的原理
例如:
- 前端server域名為:http://xx_domain
- 后端server域名為:https://github.com
現在http://xx_domain對https://github.com發起請求一定會出現跨域。
不過只需要啟動一個nginx服務器,將server_name設置為xx_domain,然后設置相應的location以攔截前端需要跨域的請求,最后將請求代理回github.com。如下面的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
## 配置反向代理的參數 server { listen 8080; server_name xx_domain ## 1. 用戶訪問 http://xx_domain,則反向代理到 https://github.com location / { proxy_pass https: //github .com; proxy_redirect off; proxy_set_header Host $host; # 傳遞域名 proxy_set_header X-Real-IP $remote_addr; # 傳遞ip proxy_set_header X-Scheme $scheme; # 傳遞協議 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } |
這樣可以完美繞過瀏覽器的同源策略:github.com訪問nginx的github.com屬于同源訪問,而nginx對服務端轉發的請求不會觸發瀏覽器的同源策略。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://juejin.im/post/5d7e49c8f265da03ce3a0884