tomcat与nginx反向代理的设置

  • Post author:
  • Post category:其他




1.首先你已经准备好了tomcat和nginx

  • 1.启动nginx
./sbin/nginx -c./conf/mynginx.conf
  • 2.消灭nginx
killall -9 nginx
  • 3.一般tomcat项目下的webapps不放在tomcat路径下,下面转移一下
cp -r webapps /application/webapps
  • nginx配置
# 修改nginx配置文件,加入upstream location

nginx -t  # 检查配置是否正确

nginx -s reload  #重新读取配置,让配置生效,不需要重启nginx


nginx参照他的配置文件

  • 5.下面是我的具体配置
worker_processes  1;
events {
        use epoll;
        worker_connections  1024;
}

http { 
        include  mime.types;
        default_type  application/octet-stream;
        client_max_body_size 300m;
        sendfile        on;
        keepalive_timeout  65;
        upstream static.itmumu.cn {
                server localhost:8888 weight=1;
        }
  
        upstream itmumu.cn {
                server localhost:8080;
                server localhost:9999;
        }
        server{
                listen 808;
                server_name static;
                location / {
                }
                location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff|doc|docx|zip){
                        root /application/www/mumu;
                        expires 30d;
                }
        }
        server {
                listen 80;
                server_name localhost;
                location / {
                        root html;
                        index index1.html index.html index.htm;
                }
                location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff){
                        proxy_cache_valid 200 304 302 5d;
                        proxy_cache_valid any 5d;
                        proxy_cache_key '$host:$server_port$request_uri';
                        add_header X-Cache '$upstream_cache_status from $host';
                        proxy_pass http://static.itmumu.cn;
                        root /application/www/mumu/;
                        expires 30d;
                }
                location ^~ /tomcat {
                        index index;
                        proxy_pass http://itmumu.cn/;
                }
        }
}
  • 注意的一点是(“~”)这个符号后面得带上空格

  • 6.tomcat需要改一下配置
找到默认的webapps,改成你放文件或者项目的路径我的是
/application/www/mumu
  • 一个问题

    • 403 forbidden nginx
  • 方案
1.权限配置不正确

这个是nginx出现403 forbidden最常见的原因。

为了保证文件能正确执行,nginx既需要文件的读权限,又需要文件所有父目录的可执行权限。
假如你的目录是/www/itmumu/cn
你得给这三个目录设置权限/www;/www/itmumu/;/ww/itmumu/cn;
chmod 755 /www
chmod 755 /www/itmumu
chmod 755 /www/itmumu/cn
解决办法:设置所有父目录为755权限,设置文件为644权限可以避免权限不正确。
2.目录索引设置错误(index指令配置)
网站根目录不包含index指令设置的文件。
例如,运行PHP的网站,通常像这样配置index
index  index.html index.htm index.php;
当访问该网站的时,nginx 会按照 index.html,index.htm ,index.php 的先后顺序在根目录中查找文件。如果这三个文件都不存在,那么nginx就会返回403 Forbidden。



版权声明:本文为qq_43655603原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。