#user nobody;
worker_processes 4;
error_log /var/log/nginx/error.log debug;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /var/run/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
#include mime.types;
#root /workspace/sources/nginx-1.13.4/html;
#default_type application/json;
# default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
#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;
tcp_nodelay on;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_body_buffer_size 512k;
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-JavaScript text/css application/xml;
gzip_vary on;
upstream cluster {
server 10.21.9.20:8081; (tomcat安装的机器IP)
server 10.21.9.20:8080;
#ip_hash;
}
server {
listen 80;
server_name 125.62.26.99; (公网IP)
index index.html index.htm;
#charset koi8-r;
(#注:下面这段location为网页访问nginx所在的本地json文件,来减少对tomcat的访问量。下面中括号[]的红色内容为网络查找.小括号内的内容不要复制到配置文件中。
[
~ ^/prefix/.*\.html$
解释:~ 表示后面跟的是正则,而且是区分大小写的( “~ ”区分大小写,“~* ”不区分大小写)。
^/prefix/.*\.html$ 就是正则表达式了,^在正则里表示 以什么开始,/prefix/ 表示符合这个文件夹路径的。 ".*" 表示匹配单个字符多次。"\." 表示转义 "." 采用 "." 本身,而非他在正则里的意思(非\r\n的单个字符)。 $ 表示已什么结尾。
//参考配置:
location ~ ^/test/.*\.(json|gif|jpg|png|jpeg|html)$ {
root /path/yourfilename/;
autoindex off;
}
在整此段时,linuxman遇到路径的问题。在location ~* /html/.*\.json$ 多加了一个/html/ 在网址访问时也要带上。下面正文去掉
)
location ~* .*\.(json|html|gif|jpg|pnp|jsp)$ {
# default_type application/json;
root /workspace/sources/nginx-1.13.4/html/;
#autoindex off;
}
location / {
root /workspace/sources/nginx-1.13.4/html/;
index index.html;
rewrite "^/(.*)$" http://125.6.2.99:80/index.html break; (此行向上两行为重定向访问的默认页面)
#access_log /usr/local/nginx/logs/host.access.log;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache_valid 200 304 12h;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://cluster;
expires 1d;
}
#location ~ .*\.(php|jsp|cgi)?$
# {
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_pass http://cluster;
#}
}
}