Nginx代理Tomcat
访问主站,Nginx提供静态页面访问,jsp do动态由Tomcat代理。下面是配置文件
[root@PYTHON wwwroot]# cat /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes 1;
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 51200;
events
{
use epoll;
worker_connections 51200;
}
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;
#limit_zone crawler $binary_remote_addr 10m;
#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;
server_name www.pirate.com 192.168.43.132;
index index.html index.htm index.php;
root /home/wwwroot;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ (\.jsp)|(\.do)$ {
proxy_pass http://192.168.43.132:8080;
proxy_redirect off;
proxy_set_header HOST $host;
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;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /nginx-status {
auth_basic "NginxStatus";
allow 192.168.43.0/24;
deny all;
stub_status on;
access_log on;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log /home/wwwlogs/access.log access;
}
include vhost/*.conf;
}
说明: 66-70行是新添进去的代理Tomcat 需要访问更多动态访问,括号添加即可
Nginx页面目录/home/wwwroot Tomcat也在这个目录下,与nginx的index.html同一级
在tomcat下面新建三个文件/home/wwwroot/tomcat/webapps/ROOT a.html a.jsp
[root@PYTHON ROOT]# cat
Tomcat " !!!!"
[root@PYTHON ROOT]# cat a.html
Tomcat a.index "a hello!!"
[root@PYTHON ROOT]# cat a.jsp
Tomcat a.jsp file "AAAAAA!!
开始测试:
访问192.168.43.132 显示nginx默认页面
访问192.168.43.132:8080
访问Tomcat默认页面没问题,现在测试jsp和do文件,用nginx是否可以代理访问
192.168.43.132/a.jsp 192.168.43.132/
访问nginx,通过配置文件的代理成功访问到tomcat目录的两个动态文件。
增加一个域名跳转命令访问ke.com 跳转到页面
ke.com 虚拟主机设置完毕之后添加 6-8行server {
listen 80;
server_name ke.com;
root html;
index index.html index.htm;
if ($host = "ke.com"){
rewrite ^/(.*)$ http:///$1 permanent;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}