nginx和tomcat做整合,nginx代理访问tomcat页面,一级页面能访问,但是二级页面提示404错误。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx和tomcat做整合,nginx代理访问tomcat页面,一级页面能访问,但是二级页面提示404错误。相关的知识,希望对你有一定的参考价值。

#user nobody;
user www www;
worker_processes 8;

error_log /usr/local/nginx/logs/error.log crit;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid /usr/local/nginx/nginx.pid;

worker_rlimit_nofile 65535;

events
use epoll;
worker_connections 65535;


http
include mime.types;
default_type application/octet-stream;
# include /usr/local/nginx/conf/proxy.conf;
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;

sendfile on;
tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_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 tomcat_server
server 192.168.0.3:8080;



#gzip on;

server
listen 80;
server_name 192.168.0.3;
index index.html index.htm index.jsp index.do default.do;
root /home/www;
if (-d $request_filename)

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;


#charset koi8-r;

#access_log logs/host.access.log main;

location ~ \.(jsp|jspx|do)?$
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_server;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

expires 30d;

location ~ .*\.(js|css)?$

expires 1h;

access_log off;

#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;
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

参考技术A user nobody;
worker_processes 2;

error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

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;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;

upstream tomcat_server
server 192.168.130.128:8080 weight=5;
server 192.168.130.129:8080 weight=5;
ip_hash;


server
listen 80;
server_name localhost;

charset utf-8;

access_log logs/host.access.log main;

location /
proxy_pass http://tomcat_server;
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;


error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;
location = /50x.html
root html;


本回答被提问者采纳

Nginx整合Tomcat

现在先不考虑集群的配置问题,只实现Nginx实现一台tomact的代理

1.我们需要一个web项目,这里我把先准备好的web.war文件部署到Tomact服务器上

mvn clean install package
mv /srv/ftp/web.war /usr/local/tomcat/webapps

2.Nginx和Tomact默认的都是80端口,所以先修改Tomcat端口

vim /usr/local/tomcat/conf/server.xml 
<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>   <!--可以自己定义端口,我这里方便自己记忆,修改的为8090端口-->

3.启动Tomcat

/usr/local/tomcat/bin/catalina.sh start

4.浏览器端输入Tomcat服务器地址,范例:

http://192.168.19.223:8090/web   <!--这里端口号改为你自己指定的端口号,后面加上项目名称-->

5.此时我们已经可以看到我们的web项目,但是tomcat和nginx还没有联系,需要我们修改nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf 
<!--修改-->
server .......
<!--配置设置在server包含下--> location / proxy_pass http://192.168.19.223:8090/; 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 ; ...............

6.对我们修改后的nginx的配置文件进行检查

/usr/local/nginx/sbin/nginx -t

如果可以通过,也可以在nginx运行的时候进行加载

/usr/local/nginx/sbin/nginx -s reload

7.现在我们可以直接在浏览器输入nginx服务器地址进行访问Tomcat

 

以上是关于nginx和tomcat做整合,nginx代理访问tomcat页面,一级页面能访问,但是二级页面提示404错误。的主要内容,如果未能解决你的问题,请参考以下文章

nginx+tomcat简单反向代理+nginx监控

nginx的反向代理功能和负载均衡

Nginx和tomcat整合

nginx反向代理配置不生效

nginx反向代理+tomcat域名绑定

Windows下Nginx+Tomcat整合的安装与配置