nginx基本配置nginx.conf解析

近期經常看到有同事部署nginx時,不會nginx的基本設置。

  下面共享了一個nginx.conf文件,並對一些常用的配置進行了簡單的說明:(基本情況是同一臺服務器上有tomcat提供的zcms或其他應用以及由nginx提供的站點靜態服務兩個,然後通過nginx的代理訪問tomcat的應用。這樣可以保證在僅開啓80端口的情況下訪問到後臺和靜態內容以及前端動態服務。)

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#user  nobody;
worker_processes  1;
#worker_processes 定義了nginx對外提供web服務時的worder進程數。
#最優值取決於許多因素,包括(但不限於)CPU核的數量、存儲數據的硬盤數量及負載模式。
#不能確定的時候,將其設置爲可用的CPU內核數將是一個好的開始(設置爲“auto”將嘗試自動檢測它)。
#worker_rlimit_nofile 更改worker進程的最大打開文件數限制。如果沒設置的話,這個值爲操作系統的限制。
#設置後你的操作系統和Nginx可以處理比“ulimit -a”更多的文件,所以把這個值設高,這樣nginx就不會有“too many open files”問題了。
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  51200;
#nginx最大連接數
}
 
 
http {
    server_tokens off;
#server_tokens 並不會讓nginx執行的速度更快,但它可以關閉在錯誤頁面中的nginx版本數字,這樣對於安全性是有好處的。
    include       mime.types;
    default_type  application/octet-stream;
 
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
 
    #access_log  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
 
    keepalive_timeout  65;
    #超時時間
    client_max_body_size 2000m;
    #提交html部分的數據大小,通常與上傳文件的大小有關
 
    gzip  on;
    gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    #gzip支持及相關類型
    ssi on;
    #ssi支持
   upstream  cms{
     server 220.181.152.146:8080;
     #若本機IP可用localhost,有時127.0.0.1無效
   }
 
 
    server {
        listen       80;
#監聽的端口
       # server_name  xxxx;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;       
    location /cms {
         index  index.html index.htm index.shtml index.zhtml;
         proxy_pass http://cms;
         proxy_redirect    off;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
             
                }
     
    location / {
            root   /usr/tomcat/webapps/wwwroot_release/news;
     #靜態目錄
            index  index.shtml index.html index.htm;
        }
        #error_page  403              /404.shtml;
        error_page  404              /404.shtml;
        #error_page  500              /500.shtml;
        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /500.shtml;
 
        #location = /500.shtml {
        #    root   shtml;
        #}
#錯誤頁面及相關信息配置
    }
  
  
#include /usr/local/nginx/vhosts/*;
#include只是一個在當前文件中包含另一個文件內容的指令。
}

  通常新部署的應用需要http下的upstream、server、location /cms、location /等幾個部分,可按照上述文件簡單修改即可。

  修改完成後可使用nginx -t 檢查正確性,nginx -s reload重新加載配置並生效。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章