由於公司使用過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 #Nginx所用用戶和組
2 #user nobody;
3 #工作的子進程數量(通常等於CPU數量或者2倍於CPU)
4 worker_processes 1;
5
6 #錯誤日志存放路徑
7 #error_log logs/error.log;
8 #error_log logs/error.log notice;
9 #error_log logs/error.log info;
10
11 #指定pid存放文件
12 #pid logs/nginx.pid;
13
14
15 events {
16 #使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue
17 #use epoll;
18
19 #使用epoll模型提高性能 win下不需要
20 #use epoll;
21 #允許最大連接數
22 worker_connections 1024;
23 }
24
25
26 http {
27 #擴展名與文件類型映射表
28 include mime.types;
29 #默認類型
30 default_type application/octet-stream;
31
32 #定義日志格式
33 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
34 # '$status $body_bytes_sent "$http_referer" '
35 # '"$http_user_agent" "$http_x_forwarded_for"';
36
37 #access_log logs/access.log main;
38
39 # 啟用內核復制模式,應該保持開啟達到最快IO效率
40 sendfile on;
41 #tcp_nopush on;
42
43 #keepalive_timeout 0;
44 # HTTP1.1支持持久連接alive
45 # 降低每個連接的alive時間可在一定程度上提高可響應連接數量,所以一般可適當降低此值
46 keepalive_timeout 65;
47
48 # 啟動gzip壓縮功能設置,有效降低網絡流量
49 gzip on;
50 gzip_min_length 1k; #最小1K
51 gzip_buffers 4 16k;
52 gzip_http_version 1.0;
53 gzip_comp_level 2;
54 gzip_types text/plain application/x-javascripttext/css application/xml;
55 gzip_vary on;
56
57 # 靜態文件緩存
58 # 最大緩存數量,文件未使用存活期
59 open_file_cache max=655350 inactive=20s;
60 # 驗證緩存有效期時間間隔
61 open_file_cache_valid 30s;
62 # 有效期內文件最少使用次數
63 open_file_cache_min_uses 2;
64
65 #xbq add
66 #upstream作負載均衡,在此配置需要輪詢的服務器地址和端口號,max_fails為允許請求失敗的次數,默認為1.
67 #weight為輪詢權重,根據不同的權重分配可以用來平衡服務器的訪問率。
68 upstream hostname {
69 server 127.0.0.1:9000 max_fails=0 weight=2;
70 server 127.0.0.1:9001 max_fails=0 weight=2;
71 }
72
73 server {
74 listen 8181;
75 server_name localhost;
76
77 #charset koi8-r;
78 #access_log logs/host.access.log main;
79
80 root /img; #在nginx-1.8.1文件夾中新建img文件夾,用於存放靜態資源
81
82 location / {
83 #root html;
84 #index index.html index.htm;
85 #xbq add
86 proxy_pass http://hostname;
87 #下面三條指令允許重新定義和添加一些將被轉移到被代理服務器的請求頭部信息
88 # 請求頭中Host信息
89 proxy_set_header Host $host;
90 # 真實的客戶端IP
91 proxy_set_header X-Real-IP $remote_addr;
92 # 代理路由信息,此處取IP有安全隱患
93 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
94 # 真實的用戶訪問協議
95 proxy_set_header X-Forwarded-Proto $scheme;
96
97 # 默認值default,
98 # 後端response 302時 tomcat header中location的host是http://192.168.1.62:8080
99 # 因為tomcat收到的請求是nginx發過去的, nginx發起的請求url host是http://192.168.1.62:8080
100 # 設置為default後,nginx自動把響應頭中location host部分替換成當前用戶請求的host部分
101 # 網上很多教程將此值設置成 off,禁用了替換,
102 # 這樣用戶浏覽器收到302後跳到http://192.168.1.62:8080,直接將後端服務器暴露給浏覽器
103 # 所以除非特殊需要,不要設置這種畫蛇添足的配置
104 proxy_redirect default;
105 client_max_body_size 10m; #允許客戶端請求的最大單文件字節數
106 client_body_buffer_size 128k; #緩沖區代理緩沖用戶端請求的最大字節數
107 proxy_connect_timeout 90; #nginx跟後端服務器連接超時時間
108 proxy_read_timeout 90; #連接成功後,後端服務器響應時間
109 proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
110 proxy_buffers 6 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置
111 proxy_busy_buffers_size 64k;#高負荷下緩沖大小(proxy_buffers*2)
112 proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
113
114 }
115
116 #xbq add
117 #配置Nginx動靜分離,定義的靜態頁面直接從/usr/nginxStaticFile(Nginx發布目錄)讀取。
118 location ~\.(gif|jpg|jpeg|png|css|js|php)$ {
119
120 #expires定義用戶浏覽器緩存的時間為7天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力 E:/staticResource;
121 expires 7d;
122 }
123
124 #xbq add
125 #啟用nginx status 監聽頁面
126 location /nginxstatus {
127 stub_status on;
128 access_log on;
129 }
130
131 #error_page 404 /404.html;
132
133 # redirect server error pages to the static page /50x.html
134 #
135 error_page 500 502 503 504 /50x.html;
136 location = /50x.html {
137 root html;
138 }
139
140 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
141 #
142 #location ~ \.php$ {
143 # proxy_pass http://127.0.0.1;
144 #}
145
146 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
147 #
148 #location ~ \.php$ {
149 # root html;
150 # fastcgi_pass 127.0.0.1:9000;
151 # fastcgi_index index.php;
152 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
153 # include fastcgi_params;
154 #}
155
156 # deny access to .htaccess files, if Apache's document root
157 # concurs with nginx's one
158 #
159 #location ~ /\.ht {
160 # deny all;
161 #}
162 }
163
164
165 # another virtual host using mix of IP-, name-, and port-based configuration
166 #
167 #server {
168 # listen 8000;
169 # listen somename:8080;
170 # server_name somename alias another.alias;
171
172 # location / {
173 # root html;
174 # index index.html index.htm;
175 # }
176 #}
177
178
179 # HTTPS server
180 #
181 #server {
182 # listen 443 ssl;
183 # server_name localhost;
184
185 # ssl_certificate cert.pem;
186 # ssl_certificate_key cert.key;
187
188 # ssl_session_cache shared:SSL:1m;
189 # ssl_session_timeout 5m;
190
191 # ssl_ciphers HIGH:!aNULL:!MD5;
192 # ssl_prefer_server_ciphers on;
193
194 # location / {
195 # root html;
196 # index index.html index.htm;
197 # }
198 #}
199
200 }