初识nginx+tomcat

百度百科说:

Nginx ("engine x") 是一个高性能的HTTP反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东新浪网易腾讯淘宝等。
 
 
贴出完整的配置文件

#运行用户
#user nobody;
#此参数修改为与CPU个数一致
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

#后添的
worker_rlimit_nofile 51200;

events {
#单个后台worker process进程的最大并发链接数
worker_connections 51200;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
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;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

#充许客户端请求的最大单个文件字节数
client_max_body_size 10m;

client_body_buffer_size 128k;

#跟后端服务器连接的超时时间 s
proxy_connect_timeout 5;

#连接成功后等候后端服务器响应时间
proxy_read_timeout 60;

#后端服务器数据回传时间
proxy_send_timeout 30;

#代理请求缓存区
proxy_buffer_size 8k;

#同上,保存用几个buffer每个最大空间是多少
proxy_buffers 4 32k;

#如果系统很忙时可以申请更大的proxy_buffers,官方推荐*2
proxy_busy_buffers_size 64k;

#缓存临时文件的大小
proxy_temp_file_write_size 128k;


#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;

#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 60;



tcp_nodelay on;
#开启gzip压缩
#gzip on;
# gzip_disable "MSIE [1-6].(?!.*SV1)";

#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 test_nginx {
#ip_hash;
least_conn; #最少连接
server 127.0.0.1:8080 max_fails=2 fail_timeout=30s;
server 192.168.137.128:8080 max_fails=2 fail_timeout=30s ;
}

server {
listen 9999;#监听的端口号 一般是80端口
server_name test_nginx;

proxy_redirect off;

access_log logs/tset_nginx.log combined;
#charset koi8-r;
#access_log logs/host.access.log main;



location / {
root /root;#定义服务器的默认网站根目录位置
index index.html index.htm;
proxy_pass http://test_nginx;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /status{
stub_status on;
access_log off;
auth_basic "NginxStatus";
auth_basic_user_file htpasswd;
}

location ~ .jsp$ {
proxy_pass http://test_nginx;
}

location ~ .(html|js|css|png|gif)$ {
#root html;
proxy_pass http://test_nginx;

# root /var/www/virtual/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}

#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 404 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;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

常用命令 1 开启nginx: start nginx    2 重新加载配置文件:nginx -s reload 3 关闭nginx  : nginx -s stop 
 
 
 
 
原文地址:https://www.cnblogs.com/syscn/p/5773762.html