1.Niginx主配置文件參數(shù)詳解
a.上面博客說(shuō)了在Linux中安裝nginx。博文地址為:http://www.cnblogs.com/hanyinglong/p/5102141.html
b.當(dāng)Nginx安裝完畢后,會(huì)有相應(yīng)的安裝目錄,安裝目錄里的nginx.confg為nginx的主配置文件,nginx主配置文件分為4部分,main(全局配置)、server(主機(jī)配置)、upstream(負(fù)載均衡服務(wù)器設(shè)置)以及l(fā)ocation(URL匹配特定位置的設(shè)置),這四者的關(guān)系是:server繼承main,location繼承server,upstream既不會(huì)繼承其它設(shè)置也不會(huì)被繼承。
c.Nginx是一個(gè)代理服務(wù)器,一般情況下,網(wǎng)站是不能部署在Nginx下的,比如用Java開(kāi)發(fā)的JavaWeb程序,我們部署在tomcat下,然后使用Nginx代理將網(wǎng)址指向tomcat即可。
2.Nginx.conf配置文件詳細(xì)說(shuō)明(附備注)
1 # kencery 注釋說(shuō)明Nginx文件
2 # 時(shí)間:2016-1-19
3 # 學(xué)習(xí)內(nèi)容,只是來(lái)自互聯(lián)網(wǎng),有版權(quán)問(wèn)題請(qǐng)聯(lián)系我刪除。
4
5 ######## Nginx的main(全局配置)文件
6 #指定nginx運(yùn)行的用戶及用戶組,默認(rèn)為nobody
7 #user nobody;
8
9 #開(kāi)啟的線程數(shù),一般跟邏輯CPU核數(shù)一致
10 worker_processes 1;
11
12 #定位全局錯(cuò)誤日志文件,級(jí)別以notice顯示,還有debug,info,warn,error,crit模式,debug輸出最多,crir輸出最少,根據(jù)實(shí)際環(huán)境而定
13 #error_log logs/error.log;
14 #error_log logs/error.log notice;
15 #error_log logs/error.log info;
16
17 #指定進(jìn)程id的存儲(chǔ)文件位置
18 #pid logs/nginx.pid;
19
20 #指定一個(gè)nginx進(jìn)程打開(kāi)的最多文件描述符數(shù)目,受系統(tǒng)進(jìn)程的最大打開(kāi)文件數(shù)量限制
21 #worker_rlimit_nofile 65535
22
23 events {
24 #設(shè)置工作模式為epoll,除此之外還有select,poll,kqueue,rtsig和/dev/poll模式
25 #use epoll;
26
27 #定義每個(gè)進(jìn)程的最大連接數(shù),受系統(tǒng)進(jìn)程的最大打開(kāi)文件數(shù)量限制。
28 worker_connections 1024;
29 }
30
31 #######Nginx的Http服務(wù)器配置,Gzip配置
32 http {
33 #主模塊指令,實(shí)現(xiàn)對(duì)配置文件所包含的文件的設(shè)定,可以減少主配置文件的復(fù)雜度,DNS主配置文件中的zonerfc1912,acl基本上都是用include語(yǔ)句。
34 include mime.types;
35
36 #核心模塊指令,智力默認(rèn)設(shè)置為二進(jìn)制流,也就是當(dāng)文件類型未定義時(shí)使用這種方式
37 default_type application/octet-stream;
38
39 #下面代碼為日志格式的設(shè)定,main為日志格式的名稱,可自行設(shè)置,后面引用
40 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
41 # '$status $body_bytes_sent "$http_referer" '
42 # '"$http_user_agent" "$http_x_forwarded_for"';
43
44 #引用日志main
45 #access_log logs/access.log main;
46
47 #設(shè)置允許客戶端請(qǐng)求的最大的單個(gè)文件字節(jié)數(shù)
48 #client_max_body_size 20M;
49 #指定來(lái)自客戶端請(qǐng)求頭的headebuffer大小
50 #client_header_buffer_size 32k;
51 #指定連接請(qǐng)求試圖寫入緩存文件的目錄路徑
52 #client_body_temp_path /dev/shm/client_body_temp;
53 #指定客戶端請(qǐng)求中較大的消息頭的緩存最大數(shù)量和大小,目前設(shè)置為4個(gè)32KB
54 #large client_header_buffers 4 32k;
55
56 #開(kāi)啟高效文件傳輸模式
57 sendfile on;
58 #開(kāi)啟防止網(wǎng)絡(luò)阻塞
59 #tcp_nopush on;
60 #開(kāi)啟防止網(wǎng)絡(luò)阻塞
61 #tcp_nodelay on;
62
63 #設(shè)置客戶端連接保存活動(dòng)的超時(shí)時(shí)間
64 #keepalive_timeout 0;
65 keepalive_timeout 65;
66
67 #設(shè)置客戶端請(qǐng)求讀取超時(shí)時(shí)間
68 #client_header_timeout 10;
69 #設(shè)置客戶端請(qǐng)求主體讀取超時(shí)時(shí)間
70 #client_body_timeout 10;
71 #用于設(shè)置相應(yīng)客戶端的超時(shí)時(shí)間
72 #send_timeout
73
74 ####HttpGZip模塊配置
75 #httpGzip modules
76 #開(kāi)啟gzip壓縮
77 #gzip on;
78 #設(shè)置允許壓縮的頁(yè)面最小字節(jié)數(shù)
79 #gzip_min_length 1k;
80 #申請(qǐng)4個(gè)單位為16K的內(nèi)存作為壓縮結(jié)果流緩存
81 #gzip_buffers 4 16k;
82 #設(shè)置識(shí)別http協(xié)議的版本,默認(rèn)為1.1
83 #gzip_http_version 1.1;
84 #指定gzip壓縮比,1-9數(shù)字越小,壓縮比越小,速度越快
85 #gzip_comp_level 2;
86 #指定壓縮的類型
87 #gzip_types text/plain application/x-javascript text/css application/xml;
88 #讓前端的緩存服務(wù)器進(jìn)過(guò)gzip壓縮的頁(yè)面
89 #gzip_vary on;
90
91 #########Nginx的server虛擬主機(jī)配置
92 server {
93 #監(jiān)聽(tīng)端口為 80
94 listen 80;
95
96 #設(shè)置主機(jī)域名
97 server_name localhost;
98
99 #設(shè)置訪問(wèn)的語(yǔ)言編碼
100 #charset koi8-r;
101
102 #設(shè)置虛擬主機(jī)訪問(wèn)日志的存放路徑及日志的格式為main
103 #access_log logs/host.access.log main;
104
105 #設(shè)置虛擬主機(jī)的基本信息
106 location / {
107 #設(shè)置虛擬主機(jī)的網(wǎng)站根目錄
108 root html;
109
110 #設(shè)置虛擬主機(jī)默認(rèn)訪問(wèn)的網(wǎng)頁(yè)
111 index index.html index.htm;
112 }
113
114 #error_page 404 /404.html;
115
116 # redirect server error pages to the static page /50x.html
117 #
118 error_page 500 502 503 504 /50x.html;
119 location = /50x.html {
120 root html;
121 }
122
123 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
124 #
125 #location ~ \.php$ {
126 # proxy_pass http://127.0.0.1;
127 #}
128
129 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
130 #
131 #location ~ \.php$ {
132 # root html;
133 # fastcgi_pass 127.0.0.1:9000;
134 # fastcgi_index index.php;
135 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
136 # include fastcgi_params;
137 #}
138
139 # deny access to .htaccess files, if Apache's document root
140 # concurs with nginx's one
141 #
142 #location ~ /\.ht {
143 # deny all;
144 #}
145 }
146
147
148 # another virtual host using mix of IP-, name-, and port-based configuration
149 #
150 #server {
151 # listen 8000;
152 # listen somename:8080;
153 # server_name somename alias another.alias;
154
155 # location / {
156 # root html;
157 # index index.html index.htm;
158 # }
159 #}
160
161
162 # HTTPS server
163 #
164 #server {
165 # listen 443 ssl;
166 # server_name localhost;
167
168 # ssl_certificate cert.pem;
169 # ssl_certificate_key cert.key;
170
171 # ssl_session_cache shared:SSL:1m;
172 # ssl_session_timeout 5m;
173
174 # ssl_ciphers HIGH:!aNULL:!MD5;
175 # ssl_prefer_server_ciphers on;
176
177 # location / {
178 # root html;
179 # index index.html index.htm;
180 # }
181 #}
182
183 }
3.Nginx代理網(wǎng)站
a.我在tomcat下部署了一個(gè)javaweb項(xiàng)目,tomcat安裝的服務(wù)器IP為:192.168.37.136,部署的項(xiàng)目在tomcat下的訪問(wèn)地址為:http://192.168.37.136:8080/lywh/
b.我在IP為192.168.37.133的服務(wù)器下面安裝成功了Nginx。
c.那怎么樣將tomcat下部署的網(wǎng)站使用Nginx代理呢?,修改Nginx的配置文件,修改命令:vim /usr/local/nginx/conf/nginx.conf
1 #user nobody;
2 worker_processes 1;
3 #error_log logs/error.log;
4 #error_log logs/error.log notice;
5 #error_log logs/error.log info;
7 #pid logs/nginx.pid;
10 events {
11 worker_connections 1024;
12 }
15 http {
16 include mime.types;
17 default_type application/octet-stream;
18
19 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
20 # '$status $body_bytes_sent "$http_referer" '
21 # '"$http_user_agent" "$http_x_forwarded_for"';
22
23 #access_log logs/access.log main;
24
25 sendfile on;
26 #tcp_nopush on;
27
28 #keepalive_timeout 0;
29 keepalive_timeout 65;
30
31 #gzip on;
32
33 #配置tomcat的IP地址和訪問(wèn)端口
34 upstream gw {
35 server 192.168.37.136:8080 weight=1;
36 }
37 server {
38 listen 80;
39 server_name localhost;
40
41 #charset koi8-r;
42
43 #access_log logs/host.access.log main;
44
45 location / {
46 root html;
47 index index.html index.htm;
48 }
49 #Nginx代理配置
50 location /lywh {
51 proxy_pass http://gw/lywh;
52 }
53 location /sapi {
54 proxy_pass http://gw/shopappapi;
55 }
56 location /cas{
57 proxy_pass http://gw/cas-server-webapp-4.0.0/login;
58 }
59 location /doc{
60 proxy_pass http://gw/docs;
61 }
62
63 #error_page 404 /404.html;
64
65 # redirect server error pages to the static page /50x.html
66 #
67 error_page 500 502 503 504 /50x.html;
68 location = /50x.html {
69 root html;
70 }
71
72 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
73 #
74 #location ~ \.php$ {
75 # proxy_pass http://127.0.0.1;
76 #}
77
78 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
79 #
80 #location ~ \.php$ {
81 # root html;
82 # fastcgi_pass 127.0.0.1:9000;
83 # fastcgi_index index.php;
84 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
85 # include fastcgi_params;
86 #}
87
88 # deny access to .htaccess files, if Apache's document root
89 # concurs with nginx's one
90 #
91 #location ~ /\.ht {
92 # deny all;
93 #}
94 }
95
96
97 # another virtual host using mix of IP-, name-, and port-based configuration
98 #
99 #server {
100 # listen 8000;
101 # listen somename:8080;
102 # server_name somename alias another.alias;
103
104 # location / {
105 # root html;
106 # index index.html index.htm;
107 # }
108 #}
109
110
111 # HTTPS server
112 #
113 #server {
114 # listen 443 ssl;
115 # server_name localhost;
116
117 # ssl_certificate cert.pem;
118 # ssl_certificate_key cert.key;
119
120 # ssl_session_cache shared:SSL:1m;
121 # ssl_session_timeout 5m;
122
123 # ssl_ciphers HIGH:!aNULL:!MD5;
124 # ssl_prefer_server_ciphers on;
125
126 # location / {
127 # root html;
128 # index index.html index.htm;
129 # }
130 #}
131
132 }
d.當(dāng)配置完Nginx.conf之后,關(guān)閉文件,執(zhí)行命令檢查配置的文件是否有問(wèn)題,如果如圖所示則說(shuō)明沒(méi)有問(wèn)題,否則需要檢查配置是否出現(xiàn)問(wèn)題
e.檢查如果返回ok,則說(shuō)明修改文件沒(méi)有出現(xiàn)任何錯(cuò)誤,這時(shí)候重啟Nginx,命令為: /usr/local/nginx/sbin/nginx -s reload
f.最后訪問(wèn)代理后的網(wǎng)站,http://192.168.37.133/lywh,如圖所示:則說(shuō)明已經(jīng)代理訪問(wèn):
這篇筆記已寫完,如果大家有什么疑問(wèn)可以和我探討,我也是一邊學(xué)習(xí)一邊寫的筆記,如哪里有錯(cuò)誤之處,請(qǐng)告知
最后祝愿大家新年快樂(lè),明天回家,旅途順利
|