Niginx+Tomcat ╰半橙微兮° 2022-09-21 15:24 82阅读 0赞 # Nginx基础 # 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。这是百度百科上反向代理的定义,如果你对该定义有些模糊的话,继续向下看。 实际上,我们配置了Nginx反向代理后,系统的物理结构可能是下面这样子的,当我们访问一个域名/IP地址时,实际访问的是我们配置的Nginx服务器,Nginx服务器的真实身份只是代理,它代理了许多不同的真正服务器(如下图中的Tomcat,Resin,IIS等)。这就是反向代理。 虽然配置反向代理比较麻烦,但是它的作用性还是很大滴。一方面是为了安全性考虑,另一方面是提供应用的访问性能。至于原理呢,本文就不详细介绍了。接下来配置Ngin的反向代理Tomcat。 1. 前期准备 nginx 安装成功 tomcat 安装成功 2. 更改nginx的配置文件 更改nginx.conf文件,在http/server/location层次结构下,添加proxy\_pass [http://localhost:7080][http_localhost_7080]; 一句话即可。注意不要放了;结尾。 upstream mytomcats \{ server 192.168.12.131:8000; server 192.168.12.132:8080; server 192.168.12.133:8090; \} server \{ listen 80; server\_name www.ixdba.net; location ~\* .(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ \{ root /web/www/html/; \} location / \{ proxy\_pass [http://mytomcats][http_mytomcats]; proxy\_redirect off; proxy\_set\_header Host host;proxysetheaderX−Real−IPremote\_addr; proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for; client\_max\_body\_size 10m; client\_body\_buffer\_size 128k; proxy\_connect\_timeout 90; proxy\_send\_timeout 90; proxy\_read\_timeout 90; proxy\_buffer\_size 4k; proxy\_buffers 432k; proxy\_busy\_buffers\_size 64k; proxy\_temp\_file\_write\_size 64k; 1. 重启nginx,在浏览器中输入nginx的访问地址,显示的就是Tomcat的访问地址。 2. 现在虽然nginx反向代理成功了,但是为了更好的发挥nginx的性能。我们将Tomcat的js、html、图片等静态文件配置到nginx上进行缓存,这样就可以提高应用的访问效率了。 3. 删除 location / 节点,避免全部请求被拦截。新增 location ~ .jsp节点和location \\.(html|js|css|png|gif|jpg) 节点,如下图。 4. 访问[http://10.0.6.108:7788][http_10.0.6.108_7788],返回nginx 404。这是因为匹配的location,应用自动跳转到nginx的404. 访问[http://10.0.6.108:7788/index.jsp][http_10.0.6.108_7788_index.jsp],返回到tomcat页面。因为jsp会自动匹配配置的location。 访问[http://10.0.6.108:7788/tomcat.png][http_10.0.6.108_7788_tomcat.png],也能正确显示tomcat的logo。说明nginx配置的静态文件location是生效的。 至此,Nginx配置反向代理Tomcat完成。 Nginx配置备份: user www www; worker_processes 2; error_log /home/wwwlogs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 655350; events { use epoll; worker_connections 1024; multi_accept on; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; 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; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; #limit_conn_zone $binary_remote_addr zone=perip:10m; ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; #log format log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; server { listen 80; #listen [::]:80 default ipv6only=on; server_name www.cheeryum.com 0.0.0.0 www.callbell.cn; charset utf-8; index index.html index.htm index.php; root /home/wwwroot; include wordpress.conf; #error_page 404 /404.html; location ~ [^/]\.php(/|$) { # comment try_files $uri =404; to enable pathinfo try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; #include pathinfo.conf; } location /nginx_status{ stub_status on; access_log off; allow 112.124.127.26; deny all; } location /clmp{ #带上客户原始请求 IP等请求信息 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #保留客户端请求的真实 IP地址,用于某些访问统计 proxy_set_header X-Real-IP $remote_addr; #保留客户端请求的域名信息 proxy_set_header Host $host; ################## 代理的相关高级配置项 ###################### #nginx跟后端服务器连接超时时间 proxy_connect_timeout 180; #连接成功后,后端服务器响应时间(代理接收超时) proxy_read_timeout 240; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffer_size 16k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_buffers 4 32k; #高负荷下缓冲大小(proxy_buffers*2) proxy_busy_buffers_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 proxy_temp_file_write_size 256k; client_max_body_size 20m; proxy_pass http://localhost:8080; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwlogs/access.log access; } include vhost/*.conf; } # Nginx+Tomcat动静分离 # ## JDK配置 ## [root@localhost ~]# tar zxvf jdk-7u45-linux-x64.tar.gz [root@localhost ~]# mv jdk1.7.0_45/ /usr/local/jdk [root@localhost ~]# vi /etc/profile JAVA_HOME=/usr/local/jdk PATH=$PATH:$JAVA_HOME/bin CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export JAVA_HOME PATHCLASSPATH [root@localhost ~]# source /etc/profile [root@localhost ~]# java -version #显示版本说明成功 java version"1.7.0_45" ## Tomcat配置 ## [root@localhost ~]# tar zxvf apache-tomcat-8.0.0-RC5.tar.gz [root@localhost ~]# mv apache-tomcat-8.0.0-RC5/usr/local/tomcat > 默认tomcat是root身份运行的,这样不安全,我们设置来用普通用户 [root@localhost ~]# groupadd tomcat [root@localhost ~]# useradd -g tomcat tomcat [root@localhost ~]# passwd tomcat [root@localhost ~]# chown tomcat.tomcat -R /usr/local/tomcat [root@localhost ~]# su - tomcat /usr/local/tomcat/bin/startup.sh [root@localhost ~]# echo "su - tomcat /usr/local/tomcat/bin/startup.sh" >> /etc/rc.local #开机启动 ## Nginx安装配置 ## [root@localhost ~]# groupadd nginx [root@localhost ~]# useradd -g nginx -s /sbin/nologin nginx [root@localhost ~]# yum install –y make zlib-devel openssl-devel pcre-devel [root@localhost ~]# tar zxvf nginx-1.4.4.tar.gz [root@localhost ~]# cd nginx-1.4.4 [root@localhost nginx-1.4.4]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module [root@localhost nginx-1.4.4]# make && make install > 主配置文件配置 [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf user nginx; worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } 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; sendfile on; keepalive_timeout 65; #gzip压缩功能设置 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-javascripttext/css application/xml; gzip_vary on; server { listen 80; server_name www.test.com; location / { #jsp网站程序根目录,一般nginx与tomcat在同一个目录 root /usr/local/tomcat/webapps/ROOT; index index.html index.jsp index.html; } location ~ .*.jsp$ { index index.jsp; proxy_pass http://127.0.0.1:8080; #来自jsp请求交给tomcat处理 proxy_redirect off; proxy_set_header Host $host; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时) proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 6 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 } location ~ .*\.(gif|jpg|png|bmp|swf)$ #由nginx处理静态页面 { expires 30d; #使用expires缓存模块,缓存到客户端30天 } location ~ .*\.( jsp|js|css)?$ { expires 1d; } error_page 404 /404.html; #错误页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } > 编写nginx启动、停止、重启等SysV管理脚本,方便使用 [root@localhost ~]# vi /etc/init.d/nginx #!/bin/bash # chkconfig: 345 99 20 # description: Nginx servicecontrol script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG echo "Nginx servicestart success." ;; stop) kill -s QUIT $(cat $PIDF) echo "Nginx service stopsuccess." ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) echo"reload Nginx configsuccess." ;; *) echo "Usage: $0{start|stop|restart|reload}" exit 1 esac [root@localhost ~]# chmod +x /etc/init.d/nginx [root@localhost ~]# service nginx restart [root@localhost ~]# chkconfig --add nginx [root@localhost ~]# chkconfig nginx on ## 性能测试 ## 下面我们使用的ab压力测试工具,模拟发起一次1万的并发请求,使用的index.html页面是百度首页代码 。。。。。 主要参数说明: Requests per second:平均每秒处理事务数 Time per request:平均事务响应时间 Tranfer rate:平均每秒吞吐量 经上面测试得出:nginx每秒能请求6000次,而tomcat只请求1000次。 nginx每秒吞吐量是3.6M,tomcat每秒吞吐量0.6M 由此看来,nginx是tomcat6倍的处理能力,如果网站程序静态页面多的话,就应该考虑使用Nginx与Tomcat整合来使用。 “\` [http_localhost_7080]: http://localhost:7080 [http_mytomcats]: http://mytomcats [http_10.0.6.108_7788]: http://10.0.6.108:7788 [http_10.0.6.108_7788_index.jsp]: http://10.0.6.108:7788/index.jsp [http_10.0.6.108_7788_tomcat.png]: http://10.0.6.108:7788/tomcat.png
还没有评论,来说两句吧...