Nginx+Tomcat配置

合集下载

nginx配置详解

nginx配置详解

nginx配置详解⾼性能的http服务代理服务器和反向代理服务器,能⽀持50000个并发连接数正向代理明确买家是谁,让代理⼈去联系⽬标例如局域⽹内的客户端想要访问局域⽹外的internet,则需要代理服务器来访问,这种代理就是正向代理反向代理客户端不需要任何配置就可以访问,只需要将请求发送到代理服务器,有代理服务器去选择⽬标服务器在返回给客户端负载均衡将请求分发到不同服务器、负载分发到不同服务器,就是负载均衡访问⼀个ip 默认是80 (nginx) tomcat 是8080动静分离为了加快⽹站的解析速度、将动态和静态页⾯有不同服务器来解析,降低原来单个服务器的压⼒安装进⼊压缩⽬录执⾏./configure执⾏make && make install (编译安装)安装完成后多⼀个local/nginx 在nginx有sbin 启动脚本./nginx访问: ->welcome to nginx查看开放的端⼝:firewall-cmd –list-all添加端⼝:Firewall-cmd –add-service=http -permanentSudo firewall-cmd –add-port=80/tcp –permanent重启防⽕墙:firewall-cmd -reloadNginx 相关命令:1 进⼊到nginx的⽬录 /usr/local/nginx/sbin2 查看nginx的版本号:./nginx -v3 启动nginx ./nginx4 关闭nginx ./nginx -s stop5 重加载 ./nginx -s reload (重新加载配置⽂件 nginx.conf)Nginx的配置⽂件组成第⼀块:全局块从配置⽂件开始到events块之间的内容、主要设置⼀些影响nginx服务器运⾏的配置指令,⽐如 worker_process 1; 值越⼤,可以⽀持的并发量也越多第⼆部分:events涉及的指令主要影响nginx服务器与⽤户的⽹络连接数⽐如 worker connections 1024 ⽀持的最⼤连接数第三部分:http块(配置最多的地⽅)http全局块server块举例:浏览器访问Nginx.conf 配置如下图:proxy_pass 反向代理的地址例如:要实现http://127.0.0.1:9091/edu 访问tomcatl1 (127.0.0.1:8080)http://127.0.0.1:8080/vod 访问tomcatl2 (127.0.0.1:8081)注意:开放端⼝ 9091 8081 8080 (firewall)在 /usr/src/tomcat-xx/apache-tomcat-xx/webapps/ 创建 edu⽬录然后放⼀个a.html⽂件这时访问就能看到这是nginx.conf ⾥要改的内容~ /edu/ #正则表达式,请求路径包含 edu时就会转发到 xxx:8080关于location的说明:负载均衡:访问http://192.168.17.129/edu/a.htmlUpstream myserver{Server 192.168.17.129:8080;Server 192.168.17.129:8081;}策略:轮询(默认)、权重、ip_hash、fair(第三⽅)轮询:服务器如果down掉,⾃动剔除权重:weight 权重越⾼,分配的客户端越多Upstream myserver{Server xxx weight=1;Server xxx weight=10;}Ip_hash:每个访客固定⼀个后端服务器、可以解决session问题Upstream myserver{Ip_hash;Server xxx;Server xxx;}Fair:按后端服务器响应时间来分配请求、响应时间断的优先分配Upstream myserverr{Server xxxx;Server xxxx;Fair;}动静分离:Nginx处理静态页⾯、tomcat处理动态页⾯:⼀:把静态⽂件独⽴成单独的域名放在独⽴的服务器上另⼀种就是动态和静态⽂件混合⼀起发布,通过nginx来分开,通过location指定不同的后缀名实现不同的转发请求、通过expires 参数设置可以使浏览器缓存过期时间,减少与服务器之间的请求和流量;设定⼀个过期时间,也就是⽆需去后端服务验证,直接通过浏览器确认是否过期即可,不会残⽣额外的流量/data/image/01.jpg/data/www/a.htmlAutoinde on; // 列出返回⽬录最终测试:http://192.168.17.129/image/01.jpghttp://192.168.17.129/www/a.html⾼可⽤集群需要两台服务器 192.168.17.129 192.168.17.131 (nginx)这两台都要安装nignx、keepalivedYum -y install keepalived安装好之后修改/etc/keepalived/keepalived.conf ⽂件分别启动nignx和keepalived./nginx systemctl start keepalived.service补充:关闭nginx服务1 kill `cat /usr/local/nginx/logs/nginx.pid` //⽂件中有个pid号2 nginx -s reload //重载Session共享:⽆论登录那⼀台ip,session是⼀致的1 内存数据库(memcache)1 修改服务器和数据库的关联关系。

LVS+NGINX+TOMCAT 集群实施操作记录

LVS+NGINX+TOMCAT 集群实施操作记录

IP:Eth0:192.168.100.115Eth1:192.168.100.215Vi /etc/init.d./lvs#!/bin/sh## lvs Start lvs## chkconfig: 2345 08 92# description: Starts, stops and saves lvs#SNS_VIP=192.168.100.215SNS_RIP1=192.168.100.114SNS_RIP2=192.168.100.113. /etc/rc.d/init.d/functions#logger $0 called with $1retval=0start(){#set squid vip/sbin/ipvsadm --set 30 5 60#/sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up#/sbin/route add -host $SNS_VIP dev eth0:0/sbin/ipvsadm -A -t $SNS_VIP:80 -s rr/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1 -g/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2 -gtouch /var/lock/subsys/ipvsadm > /dev/null 2 >&1echo "ipvsadm started"}stop(){/sbin/ipvsadm -C/sbin/ipvsadm -Z#ifconfig eth0:0 down#route del $SNS_VIPrm -rf /var/lock/subsys/ipvsadm > /dev/null 2 >&1echo "ipvsadm stoped"}status(){if [ ! -e /var/lock/subsys/ipvsadm ];thenecho "ipvsadm stoped"exit 1elseecho "ipvsadm OK"fi}case "$1" instart)start;;stop)stop;;status)status;;restart)stopstart;;*)echo $"Usage: $0 {start|stop|status}"retval=1esacexit $retvalkeepalvivedtar xf keepalived-1.2.7.tar.gzcd keepalived-1.2.7./configure --prefix=/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64Makemake installcd /etc/keepalived/mv keepalived.conf keepalived.conf.default chkconfig --add keepalivedchkconfig keepalived onvim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {notification_email {root@localhost}notification_email_from root@localhost smtp_server 127.0.0.1smtp_connect_timeout 30router_id Director1}#VRRP(虚拟路由冗余协议)实例配置vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 51priority 150advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.122.254/24 eth1}}#LVS配置virtual_server 192.168.100.215 80 {delay_loop 3lb_algo rrlb_kind DRnat_mask 255.255.255.0# persistence_timeout 50protocol TCPreal_server 192.168.100.113 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 10nb_get_retry 3delay_before_retry 3}}real_server 192.168.100.114 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 10nb_get_retry 3delay_before_retry 3}}}}LVS-realserverIp 192.168.100.113Ip 192.168.100.114Vi /etc/init.d/rsup#!/bin/bashVIP=192.168.100.215ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up #route add –host $VIP dev lo:0echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announce#sysctl –pIp 192.168.100.122(nfs)Ip 192.168.100.113Ip 192.168.100.114#安装zlibtar xzvf zlib-1.2.3.tar.gzcd zlib-1.2.3./configuremake && make install#安装pcretar zxvf pcre-7.9.tar.gzcd pcre-7.9./configure --prefix=/usr/local/pcremake && make installwget /download/nginx_mod_h264_streaming-2.2.7.tar.gztar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gzunzip nginx_upstream_check_module-master.zipmv ./nginx_upstream_check_module-master /root/healthtar -xvf nginx-1.4.1.tar.gz -C /usr/src/useradd nginxcd /usr/src/nginx-1.4.1patch -p1 < /root/health/check_1.2.6+.patch./configure --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --add-module=../nginx_mod_h264_streaming-2.2.7 --with-pcre=/softw are/pcre-7.9 --with-zlib=/software/zlib-1.2.3 --prefix=/usr/local/nginx --add-module=/root/healthmake && make install/usr/local/nginx/sbin/nginx开机自动启动vi /etc/init.d/nginx#!/bin/bash## nginx - this script starts and stops the nginx daemin# chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server# processname: nginx# config: /usr/local/nginx/conf/nginx.conf# pidfile: /usr/local/nginx/logs/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"lockfile=/var/lock/subsys/nginxstart() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6echo -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest || return $?stopstart}reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $nginx -HUPRETVAL=$?echo}force_reload() {restart}configtest() {$nginx -t -c $NGINX_CONF_FILE }rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1 }case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit 2esac############################################################################## 改权限chmod 755 /etc/init.d/nginxchkconfig nginx onchkconfig --list nginxnginx 0:off 1:off 2:on 3:on 4:on 5:on 6:offservice nginx startservice nginx stopservice nginx restartservice nginx reload/etc/init.d/nginx start/etc/init.d/nginx stop/etc/init.d/nginx restart/etc/init.d/nginx reloadNginx 配置文件vi /usr/local/nginx/conf/nginx.conf#user nginx nginx;worker_processes 12;error_log /usr/local/nginx/logs/error.log crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 65535;events{use epoll;worker_connections 204800;}http{include mime.types;default_type application/octet-stream;charset utf-8;server_names_hash_bucket_size 128;client_header_buffer_size 2k;large_client_header_buffers 4 4k;client_max_body_size 8m;sendfile on;tcp_nopush on;keepalive_timeout 60;fastcgi_cache_path /usr/local/nginx/fastcgi_temp levels=1:2keys_zone=TEST:10minactive=5m;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 1638;fastcgi_buffers 16 16k;fastcgi_busy_buffers_size 16k;fastcgi_temp_file_write_size 16k;fastcgi_cache TEST;fastcgi_cache_valid 200 302 1h;fastcgi_cache_valid 301 1d;fastcgi_cache_valid any 1m;fastcgi_cache_min_uses 1;fastcgi_cache_use_stale error timeout invalid_header http_500;open_file_cache max=204800 inactive=20s;open_file_cache_min_uses 1;open_file_cache_valid 30s;tcp_nodelay on;gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;upstream nginx_server {ip_hash;server 192.168.100.122:80;}upstream web_server {ip_hash;server 192.168.100.131:9001;server 192.168.100.132:9001;server 192.168.100.133:9001;server 192.168.100.134:9001;check interval=3000 rise=2 fall=5 timeout=1000;}upstream napi_server {ip_hash;server 192.168.100.131:9002;server 192.168.100.132:9002;server 192.168.100.133:9002;server 192.168.100.134:9002;server 192.168.100.131:9003;server 192.168.100.132:9003;server 192.168.100.133:9003;server 192.168.100.134:9003;server 192.168.100.131:9004;server 192.168.100.132:9004;server 192.168.100.133:9004;server 192.168.100.134:9004;check interval=3000 rise=2 fall=5 timeout=1000;}upstream oapi_server {ip_hash;server 192.168.100.131:9005;server 192.168.100.132:9005;server 192.168.100.133:9005;server 192.168.100.134:9005;server 192.168.100.131:9006;server 192.168.100.132:9006;server 192.168.100.133:9006;server 192.168.100.134:9006;check interval=3000 rise=2 fall=5 timeout=1000;}server {listen 80;server_name localhost;location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|html|mp3|wma|wmv)${root /usr/wodm/;proxy_pass http://nginx_server;}location ~ .*\.(js|css)?${expires 1h;}location /NginxStatus{stub_status on;access_log on;auth_basic "NginxStatus";#auth_basic_user_file conf/htpasswd;}location /nstatus {check_status;access_log off;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}log_format access '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';access_log /usr/local/nginx/logs/access.log access;include /usr/local/nginx/conf/vhost/*.conf;}Nginx(nfs)Nginx.conf#user nginx nginx;worker_processes 16;#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;error_log /usr/local/nginx/logs/error.log crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 204800;events{use epoll;worker_connections 204800;}http{include mime.types;default_type application/octet-stream;charset utf-8;server_names_hash_bucket_size 128;client_header_buffer_size 2k;large_client_header_buffers 4 4k;client_max_body_size 8m;sendfile on;tcp_nopush on;keepalive_timeout 60;fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2keys_zone=TEST:10minactive=5m;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 1638;fastcgi_buffers 16 16k;fastcgi_busy_buffers_size 16k;fastcgi_temp_file_write_size 16k;fastcgi_cache TEST;fastcgi_cache_valid 200 302 1h;fastcgi_cache_valid 301 1d;fastcgi_cache_valid any 1m;fastcgi_cache_min_uses 1;fastcgi_cache_use_stale error timeout invalid_header http_500;open_file_cache max=204800 inactive=20s;open_file_cache_min_uses 1;open_file_cache_valid 30s;tcp_nodelay on;gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;server{listen 80;server_name localhost;index index.php index.htm;root /usr/wodm/;location /status{stub_status on;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|html|mp3|wma|wmv)${expires 30d;}}log_format access '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';access_log /usr/local/nginx/logs/access.log access;标准字符集vi /etc/sysconfig/i18nLANG="zh_CN.UTF-8" SYSFONT="latarcyrheb-sun16" SUPPORTED="zh_CN.UTF-8:zh_CN:zh"LANG="zh_CN.GB18030"SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"打开文件数vi /etc/security/limits.conf* soft nofile 65535* hard nofile 65535vi /etc/pam.d/loginsession required /lib/security/pam_limits.songinx 日志按天分割vi nginx_log.sh#!/bin/bashlogs_path="/usr/local/nginx/logs/"pid_path="/usr/local/nginx/nginx.pid"mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").logkill -USR1 `cat ${pid_path}`chmod 755 nginx_log.shcrontab –e0 0 * * * bash /usr/local/nginx/nginx_log.shTomcat memcachetar xf libevent-1.4.11-stable.tar.gzcd libevent-1.4.11-stable./configuremakemake installcd ../tar xf memcached-1.4.5.tar.gzcd memcached-1.4.5./configuremakemake install/usr/local/bin/memcached -d -m 10 -u root -l 192.168.1.113 -p 11211 -c 1024 -P /tmp/memcached.pidMemcache+tomcat 共享session后续计划尚未配置。

nginx详细配置

nginx详细配置

nginx详细配置Nginx内容概览1、nginx简介(1)介绍 nginx的应⽤场景和具体可以做什么事情(2)介绍什么是反向代理(3)介绍什么是负载均衡(4)介绍什么是动静分离2、nginx安装(1)介绍 nginx在 linux系统中如何进⾏安装3、nginx常⽤的命令和配置⽂件(1)介绍 nginx启动、关闭、重新加载命令(2)介绍 nginx的配置⽂件4、nginx配置实例-反向代理5、nginx配置实例-负载均衡6、nginx配置实例-动静分离7、nginx原理与优化参数配置8、搭建 nginx⾼可⽤集群(1)搭建 nginx⾼可⽤集群(主从模式)(2)搭建 nginx⾼可⽤集群(双主模式)第 1 章 Nginx 简介1.1 Nginx 概述Nginx ("engine x") 是⼀个⾼性能的 HTTP 和反向代理服务器,特点是占有内存少,并发能⼒强,事实上 nginx的并发能⼒确实在同类型的⽹页服务器中表现较好,中国⼤陆使⽤ nginx⽹站⽤户有:百度、京东、新浪、⽹易、腾讯、淘宝等1.2 Nginx 作为 web 服务器Nginx 可以作为静态页⾯的 web 服务器,同时还⽀持 CGI 协议的动态语⾔,⽐如 perl、php等。

但是不⽀持 java。

Java程序只能通过与tomcat配合完成。

Nginx专为性能优化⽽开发,性能是其最重要的考量,实现上⾮常注重效率,能经受⾼负载的考验,有报告表明能⽀持⾼达50,000个并发连接数。

1.3 正向代理Nginx 不仅可以做反向代理,实现负载均衡。

还能⽤作正向代理来进⾏上⽹等功能。

正向代理:如果把局域⽹外的 Internet 想象成⼀个巨⼤的资源库,则局域⽹中的客户端要访问Internet,则需要通过代理服务器来访问,这种代理服务就称为正向代理。

1.4 反向代理反向代理,其实客户端对代理是⽆感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择⽬标服务器获取数据后,在返回给客户端,此时反向代理服务器和⽬标服务器对外就是⼀个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP地址。

linux下Nginx+tomcat整合的安装与配置

linux下Nginx+tomcat整合的安装与配置

目的:搭建Nginx与tomcat整合,用Nginx代替apache步骤:一、安装Nginx1、上传nginx-0.7.63.tar.gz至/usr/local2、执行如下命令解压nginx:1.#cd /usr/local2.#tar zxvf nginx-0.7.63.tar.gz3、编译安装nginx1.#cd nginx-0.7.632.#./configure --with-http_stub_status_module --with-http_ssl_module #启动server状态页和https模块执行完后会提示一个错误,说缺少PCRE library 这个是HTTP Rewrite 模块,也即是url静态化的包可上传pcre-7.9.tar.gz,输入如下命令安装:1.#tar zxvf pcre-7.9.tar.gz2.#cd pcre-7.93.#./configure4.#make5.#make install安装pcre成功后,继续安装nginx如果是ubuntu 或Debian系统请先安装# apt-get install libpcre3 libpcre3-dev原文:I need to compile few application and I need Perl 5 Compatible Regular Expression Library (PCRE). Under CentOS I can use a package called pcre-devel, but Debian do not have the same. How do I install pcre-devel under Debian / Ubuntu Linux?Perl-compatible regular expression library. PCRE has its own native API, but a set of "wrapper"functions that are based on the POSIX API are also supplied in the library libpcreposix. Note that this just provides a POSIX calling interface to PCRE: the regular expressions themselves still follow Perl syntax and semantics. The header file for the POSIX-style functions is called pcreposix.h. To install PCRE, type thy following command:# apt-get update# apt-get install libpcre3 libpcre3-dev1.#cd nginx-0.7.632.#./configure3.#make4.#make install4、nginx安装成功后的安装目录为/usr/local/nginx在conf文件夹中新建proxy.conf,用于配置一些代理参数,内容如下:01.#!nginx (-)02.# proxy.conf03.proxy_redirect off;04.proxy_set_header Host $host;05.proxy_set_header X-Real-IP $remote_addr; #获取真实ip06.#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip07.client_max_body_size 10m;08.client_body_buffer_size 128k;09.proxy_connect_timeout 90;10.proxy_send_timeout 90;11.proxy_read_timeout 90;12.proxy_buffer_size 4k;13.proxy_buffers 4 32k;14.proxy_busy_buffers_size 64k;15.proxy_temp_file_write_size 64k;编辑安装目录下conf文件夹中的nginx.conf,输入如下内容001.#运行nginx所在的用户名和用户组002.#user www www;003.004.#启动进程数005.worker_processes 8;006.#全局错误日志及PID文件007.error_log /usr/local/nginx/logs/nginx_error.log crit;008.009.pid /usr/local/nginx/nginx.pid;010.011.#Specifies the value for maximum file descriptors that can be opened by this process. 012.013.worker_rlimit_nofile 65535;014.#工作模式及连接数上限015.events016.{017. use epoll;018. worker_connections 65535;019.}020.#设定http服务器,利用它的反向代理功能提供负载均衡支持021.http022.{023. #设定mime类型024. include mime.types;025. default_type application/octet-stream;026. include /usr/local/nginx/conf/proxy.conf;027. #charset gb2312;028. #设定请求缓冲029. server_names_hash_bucket_size 128;030. client_header_buffer_size 32k;031. large_client_header_buffers 4 32k;032. client_max_body_size 8m;033.034. sendfile on;035. tcp_nopush on;036.037. keepalive_timeout 60;038.039. tcp_nodelay on;040.041.# fastcgi_connect_timeout 300;042.# fastcgi_send_timeout 300;043.# fastcgi_read_timeout 300;044.# fastcgi_buffer_size 64k;045.# fastcgi_buffers 4 64k;046.# fastcgi_busy_buffers_size 128k;047.# fastcgi_temp_file_write_size 128k;048.049.# gzip on;050.# gzip_min_length 1k;051.# gzip_buffers 4 16k;052.# gzip_http_version 1.0;053.# gzip_comp_level 2;054.# gzip_types text/plain application/x-javascript text/css application/xml;055.# gzip_vary on;056.057. #limit_zone crawler $binary_remote_addr 10m;058. ###禁止通过ip访问站点059. server{060. server_name _;061. return 404;062. }063.064.065. server066. {067. listen 80;068. server_name localhost;069. index index.html index.htm index.jsp;#设定访问的默认首页地址070. root /home/www/web/ROOT;#设定网站的资源存放路径071.072. #limit_conn crawler 20;073.074. location ~ .*.jsp$ #所有jsp的页面均交由tomcat处理075. {076. index index.jsp;077. proxy_pass http://localhost:8080;#转向tomcat处理078. }079.080.081. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #设定访问静态文件直接读取不经过tomcat 082. {083. expires 30d;084. }085.086. location ~ .*\.(js|css)?$087. {088. expires 1h;089. }090.091.#定义访问日志的写入格式092. log_format access '$remote_addr - $remote_user [$time_local] "$request" '093. '$status $body_bytes_sent "$http_referer" '094. '"$http_user_agent" $http_x_forwarded_for';095. access_log /usr/local/nginx/logs/localhost.log access;#设定访问日志的存放路径096.097. }098.099.100.101.102.}5、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:1.#/usr/local/nginx/sbin/nginx -t如果屏幕显示以下两行信息,说明配置文件正确:1.the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok2.the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully如果提示unknown host,则可在服务器上执行:ping 如果也是同样提示unknown host则有两种可能:a、服务器没有设置DNS服务器地址,查看/etc/resolv.conf下是否设置,若无则加上b、防火墙拦截6、启动nginx的命令1.#/usr/local/nginx/sbin/nginx这时,输入以下命令查看Nginx主进程号:1.ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'7、停止nginx的命令1.#/usr/local/nginx/sbin/nginx -s stop8、在不停止Nginx服务的情况下平滑变更Nginx配置a、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:1./usr/local/nginx/sbin/nginx -t如果屏幕显示以下两行信息,说明配置文件正确:1.the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok2.the configuration file /usr/local/nginx/conf/nginx.conf was tested successfullyb、这时,输入以下命令查看Nginx主进程号:1.ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'屏幕显示的即为Nginx主进程号,例如:6302这时,执行以下命令即可使修改过的Nginx配置文件生效:1.kill -HUP 6302或者无需这么麻烦,找到Nginx的Pid文件:1.kill -HUP `cat /usr/local/nginx/nginx.pid`9、nginx启动好后启动tomcat,此时输入http://主机ip地址即可看到“My web!”三、其他stub_status语法: stub_status on默认值: None作用域: location创建一个location 区域启用stub_status"stub status" 模块返回的状态信息跟mathopd's 的状态信息很相似. 返回的状态信息如下:1.Active connections: 2912.server accepts handled requests3.16630948 16630948 310704654.Reading: 6 Writing: 179 Waiting: 106active connections -- 对后端发起的活动连接数server accepts handled requests -- nginx 总共处理了16630948 个连接, 成功创建16630948 次握手(证明中间没有失败的), 总共处理了31070465 个请求(平均每次握手处理了 1.8个数据请求)reading -- nginx 读取到客户端的Header信息数writing -- nginx 返回给客户端的Header信息数waiting -- 开启keep-alive 的情况下,这个值等于active - (reading + writing),意思就是Nginx 说已经处理完正在等候下一次请求指令的驻留连接。

Nginx+tomcat+ssl安装配置手册

Nginx+tomcat+ssl安装配置手册

Nginx + tomcat + SSL 安装配置手册1.介绍Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器。

nginx有以下几项基本特性:模块化结构过滤器包括gzipping, byte ranges, chunked responses,以及 SSI-filter等filter。

高性能支持内核Poll模型,能经受高负载的考验,有报告表明能支持高达 50,000个并发连接数。

高稳定性Nginx采取了分阶段资源分配技术,使得它的CPU与内存占用率非常低。

官方表示保持10,000个没有活动的连接,它只占2.5M内存。

多负载策略多种分配策略,并且分配均匀。

自Nginx 发布四年来,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。

2.准备2.1 资源Nginx中文网站:/Nginx英文网站:/PCRE网站:/2.2 相关软件1)PCREPCRE(Perl Compatible Regular Expressions)中文含义:perl语言兼容正则表达式)是一个用C语言编写的正则表达式函数库。

neginx中使用正则表达式进行灵活配置,安装之前需要确认PCRE已安装。

下载地址:/,使用版本pcre-8.12.tar.gz2)nginx-upstream-jvm-routenginx_upstream_jvm_route 是一个 Nginx 的扩展模块,用来实现基于Cookie 的Session Sticky 的功能。

下载地址(svn):/svn/trunk/3.4.4.1 Windows版安装安装文件为.zip文件,解压缩后,运行目录中的nginx.exe(或使用命令),服务启动。

Nginx反向代理Tomcat服务器

Nginx反向代理Tomcat服务器

一、Tomcat基本配置1.为Tomcat提供SysV脚本2.catalina 脚本讲解3.telnet 登录管理Tomcat4.配置Tomcat虚拟主机5.Tomcat图形管理接口6.部署JSP网站案例二、Nginx反向代理Tomcat服务器1.Nginx将请求反向代理到后端Tomcat2.Nginx将图片缓存到本地3.Nginx将请求实现动静分离注,实验环境说明,操作系统:CentOS 6.4 x86_64,软件版本:jdk-7u40、apache-tomcat-7.0.42、Nginx-1.4.2,博客中所用到的软件请到这里下载:/QGBCLwrZnpLMS。

一、Tomcat 基本配置1.为Tomcat提供SysV脚本注,在上一篇博文中我们已经演示安装了Tomcat,这里我们就不在演示,不清楚的博友可以参考这篇博文,/2033581/1299644,在上一篇博文中我们没有增加,SysV脚本,在这篇博文中我们来增加一下,下面我们就来具体演示一下。

[root@tomcat ~]# vim /etc/init.d/tomcat#!/bin/sh# Tomcat init script for Linux.## chkconfig: 2345 96 14# description: The Apache Tomcat servlet/JSP container.CATALINA_HOME=/usr/local/tomcat #注意你的脚本路径export CATALINA_HOME# export CATALINA_OPTS="-Xms128m -Xmx256m"exec $CATALINA_HOME/bin/catalina.sh $*下面我们来增加执行权限,并加入服务列表设置开机自启动,[root@tomcat ~]# chmod +x /etc/init.d/tomcat[root@tomcat ~]# chkconfig --add tomcat[root@tomcat ~]# chkconfig tomcat --listtomcat 0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭下面我们来启动一下Tomcat并测试一下,[root@tomcat ~]# service tomcat startUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usrUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/to mcat-juli.jar查看一下启动的端口号,[root@tomcat ~]# netstat -ntulpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Progra m nametcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1044/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1121/mastertcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 12988/sshdtcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 13053/sshdtcp 0 0 :::8080 :::* LISTEN 13088/javatcp 0 0 :::22 :::* LISTEN 1044/sshdtcp 0 0 ::1:25 :::* LISTEN 1121/mastertcp 0 0 ::1:6010 :::* LISTEN 12988/sshdtcp 0 0 ::1:6011 :::* LISTEN 13053/sshdtcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 13088/javatcp 0 0 :::8009 :::* LISTEN 13088/java用浏览器访问一下,好了,到这里Tomcat的SysV脚本增加完成,下面我们来说一下catalina脚本。

HTTP2.0学习与Nginx和Tomcat配置HTTP2.0

HTTP2.0学习与Nginx和Tomcat配置HTTP2.0

HTTP2.0学习与Nginx和Tomcat配置HTTP2.0⽬录⼀、HTTP2.01.1 简介1.2 新的特性1.3 h2c 的⽀持度⼆、Nginx 对 http2.0 的⽀持2.1 Nginx 作为服务端使⽤http2.02.2 Nginx 作为客户端使⽤ http2.0三、Tomcat 对 HTTP2.0 的⽀持3.1.1、依赖环境3.1.2、h2c 配置(⾮加密)3.1.3、h2 配置(加密)3.1 、Tomcat 8.5四、扩展问题解决⽅法⼀(没⾏通)⽅法⼆(可⾏)4.1、测试 h2c4.2、查看浏览器是否⽀持 http2.04.3、查看⽹站是否⽀持 http2.04.4、JAVA8 如何⽀持 HTTP2.0 TLS⼀、HTTP2.01.1 简介HTTP/2(超⽂本传输协议第2版,最初命名为HTTP 2.0),简称为h2(基于TLS/1.2或以上版本的加密连接)或h2c(⾮加密连接),是HTTP协议的的第⼆个主要版本。

1.2 新的特性具体可以看这篇⽂章:https:///a/11900000134207841. 头数据压缩 Data compression of HTTP headers2. 服务器推送 HTTP/2 Server Push3. 管线化请求 Pipelining of requests.4. 对数据传输采⽤多路复⽤,让多个请求合并在同⼀ TCP 连接内 Multiplexing multiple requests over a single TCP connection,因为每⼀个tcp 连接在创建的时候都需要耗费资源,⽽且在创建初期,传输也是⽐较5. 采⽤了⼆进制⽽⾮明⽂来打包、传输客户端<——>服务器间的数据。

1.3 h2c 的⽀持度HTTP/2 的设计本⾝允许⾮加密的 HTTP 协议,也允许使⽤TLS 1.2或更新版本协议进⾏加密。

协议本⾝未要求必须使⽤加密,惟多数客户端 (例如 Firefox, Chrome, Safari, Opera, IE, Edge) 的开发者声明,他们只会实⼆、Nginx 对 http2.0 的⽀持2.1 Nginx 作为服务端使⽤http2.0使⽤ http2.0 的条件1. Nginx 版本⼤于或等于 1.9.5 。

Nginx配置实例-反向代理实现浏览器请求Nginx跳转到服务器某页面

Nginx配置实例-反向代理实现浏览器请求Nginx跳转到服务器某页面

Nginx配置实例-反向代理实现浏览器请求Nginx跳转到服务器某页⾯场景Ubuntu Server 16.04 LTS上怎样安装下载安装Nginx并启动:Nginx的配置⽂件位置以及组成部分结构讲解:Linux-Ubuntu Server 16.04安装JDK以及配置JDK环境变量:下⾯实例实现客户端浏览器请求指定⽹址,跳转到服务器Tomcat主页⾯。

所以需要参考上⾯去实现⼀些准备⼯作。

注:实现安装JDK使⽤VmWare安装Ubuntu Server 16,然后安装JDK以及环境变量参考下⾯:Linux-Ubuntu Server 16.04安装JDK以及配置JDK环境变量:效果安装Tomcat下载Tomcat的tar包,⾃⾏去官⽹下载,这⾥是apache-tomcat-7.0.70.tar.gz然后使⽤Xftp或者rz命令将Tomcat的tar包上传到 /usr/local ⽬录下然后将Tomcat的tar包解压tar -xzvf apache-tomcat-7.0.70.tar.gzx代表解压 z代表gz压缩 v显⽰解压列表 f解压⽂件名字然后进⼊到解压的⽬录下的bin下的⽬录启动Tomcat./startup.sh效果打开浏览器输⼊:ip:8080反向代理实现流程客户端浏览器访问,会映射到Nginx服务器上,然后通过反向代理去请求tomcat的主页⾯。

配置域名映射ip在C:\Windows\System32\drivers\etc下的host⽂件中配置映射使⽤编辑器打开,添加ip与域名的映射保存后,使⽤浏览器访问:如果出现Tomcat的主页⾯则说明映射成功。

虽然这样能直接通过8080端⼝直接访问,但是咱的⽬的是通过Nginx反向代理访问Tomcat的主页⾯,即通过80端⼝访问⽽80端⼝⼜是默认端⼝,所以只需要域名就能访问Tomcat的主页⾯。

在Nginx进⾏反向代理的配置参考下⾯这篇⽂章,找到Nginx安装⽬录下的配置⽂件Ubuntu Server 16.04 LTS上怎样安装下载安装Nginx并启动:默认安装路径 /usr/local/nginx/conf/nginx.conf然后编辑配置⽂件vi nginx.conf下⾯是默认的配置⽂件内容下⾯要修改两个地⽅然后保存。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Nginx+Tomcat配置为什么使用Nginx?Apache∙经典的Web服务器∙除了慢没有别的缺点了∙Apache2对fcgi支持并不好∙非常好用的proxy和proxy_ajp(很多人用它作为tomcat的前端)∙不支持epoll(这年头,epoll几乎是性能的必备)Nginx∙速度快,占用资源少∙杀手级的proxy和rewrite∙非常不错的静态文件能力∙最适合作为整个网站的前端服务(将php、svn等不同请求发送往后端apache)∙其他功能马马虎虎代理服务器,也是一个 IMAP/POP3/SMTP代理服务器。

已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而被人们广泛使用了。

大多数人选择它主要考虑到的是它的高并发和负载均衡。

安装Nginx1. tar zxvf nginx0.8.36.tar.gz2、编译安装nginxcd nginx-0.7.44./configure --with-http_stub_status_module --with-http_ssl_module --prefix=/usr/local/nginx执行后,可能会提示 ./configure: error: the HTTP rewrite module requires the PCRE library.这个错误,是缺少pcre 包,可用yum install pcre pcre-devlel来解决3、make && make install4、nginx安装成功后的安装目录为/usr/local/nginx5.编辑配置文件nginx.conf#user www www;worker_processes 8; #启动进程数,可根据机器核数来修改error_log /usr/local/nginx/logs/nginx_error.log crit; #全局错误日志及PID文件pid /usr/local/nginx/nginx.pid;worker_rlimit_nofile 65535;events{use epoll;worker_connections 65535; #工作模式及连接数上限}http #设定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; #定义的日志格式和存放路径#General Optionsserver_names_hash_bucket_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_body_buffer_size 8m; #256kserver_tokens off;ignore_invalid_headers on;recursive_error_pages on;server_name_in_redirect off;sendfile on;#timeoutskeepalive_timeout 60;#client_body_timeout 3m;#client_header_timeout 3m;#send_timeout 3m;#TCP Optionstcp_nopush on;tcp_nodelay on;#size limitsclient_max_body_size 50m;gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;upstream {ip_hash;server x.x.x.x:8080 max_fails=0 weight=1;server x.x.x.x:8080 max_fails=0 weight=1; #8080为tomcat端口 }server {listen 80 default;rewrite ^(.*) / permanent;#charset koi8-r;#access_log logs/host.access.log main;#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;}}server{listen 80;server_name ;index index.php index.html index.htm;root /http;access_log /data/logs/.log combined;error_log /data/logs/error_test.xxx.log;#expireslocation ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location ~ .*\.(js|css)?${expires 24h;}location /nginxstatus {stub_status on;access_log off;}# location ~ .*\.jsp?$location /{proxy_pass http://npduxiu;proxy_redirect off;# proxy_set_header X-Real-IP $remote_addr;proxy_set_header Host $http_host;proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_header Connection Close;proxy_set_header X-Forwarded-For $remote_addr; error_page 404 /404.html;error_page 500 502 503 504 /50x.html;# location = /50x.html {# root html;# }}}}6、修改/usr/local/nginx/conf/nginx.conf配置文件后,用命令/usr/local/nginx/sbin/nginx -t 检查配置文件是否正确:7、启动nginx的命令/usr/local/nginx/sbin/nginx8、停止nginx的命令/usr/local/nginx/sbin/nginx -s stop9、在不停止Nginx服务的情况下加载Nginx配置kill -HUP `cat /usr/local/nginx/nginx.pid`10.nginx网站开启后(已启用stub_stauts),可用/nginxstatus查看nginx状态active connections -- 对后端发起的活动连接数server accepts handled requests -- nginx 总共处理的连接, 成功创建了几次握手总共处理了多少个请求reading -- nginx 读取到客户端的Header信息数writing -- nginx 返回给客户端的Header信息数waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing)安装Tomcat下面开始Tomcat的安装了,那就更简单了,网上文档也是一大把Tomcat安装一、jdk的安装安装的jdk为:jdk-6u21-linux-x64.bin1.sh jdk-6u17-linux-x64-rpm.bin2.安装程序在问您是否愿意遵守刚才看过的许可协议。

输入"y" 或"yes" 回车3.执行后,把后成的文件夹命名为jdk,放到/usr/local/下(路径可自己选,但在变量中要指出)4.vi /etc/profile在里面添加如下内容export JAVA_HOME=/usr/local/jdkexport JAVA_BIN=/usr/local/jdk/binexport PATH=$PATH:$JAVA_HOME/binexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar5. source /etc/profile 使刚才填加的生效6.验证java -version屏幕输出:java version "1.6.0_13"Java(TM) SE Runtime Environment (build 1.6.0_13-b03)Java HotSpot(TM) Server VM (build 11.3-b02, mixed mode)安装JDK1.6完毕.二、Tomcat的安装下载apache-tomcat-6.0.18.tar.gz,解压后更名或更改存放路径(可自行规定)nginx与tomcat的结合,主要用的是nginx中的upstream,后端可包括有多台tomcat来处ginx的upstream目前支持4种方式的分配1、轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2、weight指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

例如:upstream bakend {server 192.168.0.14 weight=10;server 192.168.0.15 weight=10;}2、ip_hash每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。

相关文档
最新文档