由于公司使用過Ngnix,對于剛接觸Nginx來說,感覺有些好奇,于是研究了下。
本人在windows下使用的版本是nginx-1.8.1:
1. 啟動Ngnix
雙擊nginx-1.8.1文件夾中nginx.exe,當任務管理器中存在兩個nginx進程時,則說明啟動成功!
2. Ngnix常用命令
- nginx -s stop 強制關閉
- nginx -s quit 安全關閉
- nginx -s reload 改變配置文件的時候,重啟nginx工作進程,來時配置文件生效
- nginx -s reopen 打開日志文件
3. Nginx配置
下面配置綜合了網上的資料,記下,防止自己忘記。
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#Nginx所用用戶和組 #user nobody; #工作的子進程數量(通常等于CPU數量或者2倍于CPU) worker_processes 1; #錯誤日志存放路徑 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #指定pid存放文件 #pid logs/nginx.pid; events { #使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue #use epoll; #使用epoll模型提高性能 win下不需要 #use epoll; #允許最大連接數 worker_connections 1024; } http { #擴展名與文件類型映射表 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; # 啟用內核復制模式,應該保持開啟達到最快IO效率 sendfile on; #tcp_nopush on; #keepalive_timeout 0; # HTTP1.1支持持久連接alive # 降低每個連接的alive時間可在一定程度上提高可響應連接數量,所以一般可適當降低此值 keepalive_timeout 65; # 啟動gzip壓縮功能設置,有效降低網絡流量 gzip on; gzip_min_length 1k; #最小1K gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text /plain application /x-javascripttext/css application /xml ; gzip_vary on; # 靜態文件緩存 # 最大緩存數量,文件未使用存活期 open_file_cache max=655350 inactive=20s; # 驗證緩存有效期時間間隔 open_file_cache_valid 30s; # 有效期內文件最少使用次數 open_file_cache_min_uses 2; #xbq add #upstream作負載均衡,在此配置需要輪詢的服務器地址和端口號,max_fails為允許請求失敗的次數,默認為1. #weight為輪詢權重,根據不同的權重分配可以用來平衡服務器的訪問率。 upstream hostname { server 127.0.0.1:9000 max_fails=0 weight=2; server 127.0.0.1:9001 max_fails=0 weight=2; } server { listen 8181; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; root /img ; #在nginx-1.8.1文件夾中新建img文件夾,用于存放靜態資源 location / { #root html; #index index.html index.htm; #xbq add proxy_pass http: //hostname ; #下面三條指令允許重新定義和添加一些將被轉移到被代理服務器的請求頭部信息 # 請求頭中Host信息 proxy_set_header Host $host; # 真實的客戶端IP proxy_set_header X-Real-IP $remote_addr; # 代理路由信息,此處取IP有安全隱患 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 真實的用戶訪問協議 proxy_set_header X-Forwarded-Proto $scheme; # 默認值default, # 后端response 302時 tomcat header中location的host是http://192.168.1.62:8080 # 因為tomcat收到的請求是nginx發過去的, nginx發起的請求url host是http://192.168.1.62:8080 # 設置為default后,nginx自動把響應頭中location host部分替換成當前用戶請求的host部分 # 網上很多教程將此值設置成 off,禁用了替換, # 這樣用戶瀏覽器收到302后跳到http://192.168.1.62:8080,直接將后端服務器暴露給瀏覽器 # 所以除非特殊需要,不要設置這種畫蛇添足的配置 proxy_redirect default; client_max_body_size 10m; #允許客戶端請求的最大單文件字節數 client_body_buffer_size 128k; #緩沖區代理緩沖用戶端請求的最大字節數 proxy_connect_timeout 90; #nginx跟后端服務器連接超時時間 proxy_read_timeout 90; #連接成功后,后端服務器響應時間 proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小 proxy_buffers 6 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置 proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大于這個值,將從upstream服務器傳 } #xbq add #配置Nginx動靜分離,定義的靜態頁面直接從/usr/nginxStaticFile(Nginx發布目錄)讀取。 location ~\.(gif|jpg|jpeg|png|css|js|php)$ { #expires定義用戶瀏覽器緩存的時間為7天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力 E:/staticResource; expires 7d; } #xbq add #啟用nginx status 監聽頁面 location /nginxstatus { stub_status on; access_log on; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x .html; location = /50x .html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/xbq8080/p/6117197.html