{"success":true,"code":0,"msg":"","data":{"themeEx":{"tid":4,"replies":0,"views":5762,"searchs":7,"topGmtUpdate":null,"top":false},"theme":{"id":4,"title":"在Centos7中为Tomcat配置nginx反向代理","content":"
Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
如果把多个项目放在一个tomcat里,一个项目重启会影响到其他项目。部署的时候目录切来切去也容易搞错。还有一点,如果只是用Tomcat,一台服务器只能对应一个域名,所以需要Nginx来配置多域名访问。
请确保登录用户有root权限,本文中的操作均在root用户下进行。本文中使用的是Centos7.
因为yum中默认的Ngnix版本是1.6,这里我们先更新下版本。
查看版本命令:yum info ngnix
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
等待安装完成即可
yum install -y nginx
systemctl start nginx
如果一切进展顺利的话,现在你可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面;
如果看到这个页面,那么说明你的Nginx已经正确安装。
systemctl enable nginx
查看是否设置成功:
systemctl is-enabled nginx
如果显示enabled,则设置开机启动成功。
网站文件存放默认目录
/usr/share/nginx/html
网站默认站点配置
/etc/nginx/conf.d/default.conf
自定义Nginx站点配置文件存放目录
/etc/nginx/conf.d/
Nginx全局配置
/etc/nginx/nginx.conf
在这里你可以改变设置用户运行Nginx守护程序进程一样,和工作进程的数量得到了Nginx正在运行,等等。
如果原来的tomcat为80端口,需要换成其他的,如:8080,8081等等
如果有多个Tomcat,可以在/etc/nginx/conf.d/下创建多个conf,如:tomcat1.conf,tomcat2.conf....
配置内容:
server { \n listen 80; \n server_name www.reinforce.cn; \n location / { \n proxy_pass http://localhost:8081; \n proxy_set_header Host $host; \n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; \n } \n}
nginx -t
显示以下信息表示配置OK:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok\nnginx: configuration file /etc/nginx/nginx.conf test is successful
nginx -s reload\nsystemctl restart nginx
输入网址,妥妥的成功了(o゜▽゜)o☆
---------------------------------------2016-12-05--------------------------------------------
因为要上传文件,但是发现用了nginx之后上传失败了,F12看一下,报错:
Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
上传文件太大...因为nginx默认只能上传1M的文件。
所以,修改nginx配置文件nginx.conf,找到找到 http{} 段,在里面加一行 client_max_body_size 10m;
这样把最大改成10M,就OK了~