当前位置:   首页安装配置

Nginx配合Apache或Tomcat的动静分离基本配置实例

发布日期:2022-06-09 09:52 | 文章来源:源码中国

其实本人比较喜欢nginx跑静态和做负载反向代理,动态php还是交给apache处理比较稳定,jsp就交给tomcat、resin或jboss。nginx跑静态的能力是无与伦比的,是目前web主机里最强的。nginx和apache、tomcat、resin的动静分离配置其实很简单,就几句配置,稳定性也非常好。

1、nginx和apache的动静分离配置:

把下面配置放到nginx配置文件相应的server { }里面,如果使用其他端口号,改一下就行:

#所有php的动态页面均交由apache处理

location ~ \.(php)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:88;
}

#所有静态文件由nginx直接读取不经过apache

location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*\.(js|css)?$
{ expires 1h; }

如果之前设置了FastCGI的,把下面的配置注释掉:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /var/www/html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# include fastcgi.conf;
#}


重启nginx就生效,如图所示,标头显示nginx,phpinfo里面显示是apache,说明动静分离生效。

2.niginx和tomcat的动静分离配置:
#主配置文件配置

[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_se

#编写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
联系我们
关于使用场景和技术架构的更多咨询,请联系我们的销售和技术支持团队。
Yingsoo Host

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部