搜索
首页运维NginxNginx配合Apache或Tomcat的动静分离怎么配置

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,说明动静分离生效。

Nginx配合Apache或Tomcat的动静分离怎么配置

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_sent"$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';
  access_log logs/access.log main;
  sendfile    on;
  keepalive_timeout 65;
  #gzip压缩功能设置
  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-javascripttext/css application/xml;
  gzip_vary on;
  server {
    listen    80;
    server_name www.test.com;
    location / {
  #jsp网站程序根目录,一般nginx与tomcat在同一个目录
      root /usr/local/tomcat/webapps/root;
      index index.html index.jsp index.html;
    }
    location ~ .*.jsp$ {
    index index.jsp;
    proxy_pass http://127.0.0.1:8080;  #来自jsp请求交给tomcat处理
    proxy_redirect off;
    proxy_set_header host $host;  #后端的web服务器可以通过x-forwarded-for获取用户真实ip
    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;  #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_read_timeout 90;   #连接成功后,后端服务器响应时间(代理接收超时)
    proxy_buffer_size 4k;    #设置代理服务器(nginx)保存用户头信息的缓冲区大小
    proxy_buffers 6 32k;    #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
    proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2)
    proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
    }
    location ~ .*\.(gif|jpg|png|bmp|swf)$  #由nginx处理静态页面
    {
    expires 30d;  #使用expires缓存模块,缓存到客户端30天
    }
    location ~ .*\.( jsp|js|css)?$
    {
    expires 1d;
    }
    error_page 404       /404.html;  #错误页面
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }
  }

#编写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

以上是Nginx配合Apache或Tomcat的动静分离怎么配置的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
nginx怎么部署前端项目nginx怎么部署前端项目Apr 14, 2025 am 10:30 AM

通过以下步骤在 Nginx 中部署前端项目:创建项目结构,将代码分为 public 和 src 目录。编译源代码(可选)。配置 Nginx 服务器块,指定根目录和域名。启用服务器块。重新加载 Nginx。访问已部署的项目。

nginx怎么跳转nginx怎么跳转Apr 14, 2025 am 10:27 AM

通过 Nginx 进行重定向可将请求路由到不同 URL。具体的步骤包括:配置服务器块,指定监听端口和服务器名称。使用 rewrite 指令指定重定向类型,如永久重定向 (301) 或临时重定向 (302)。使用正则表达式匹配请求,并指示重定向的 URL。通过浏览器或 curl 命令测试重定向,检查响应代码是否与预期一致,请求是否正确重定向。

nginx怎么启用nginx怎么启用Apr 14, 2025 am 10:24 AM

Nginx(一种流行的开源 Web 服务器)可以通过以下步骤启用:Linux:安装 Nginx,启动服务,检查服务状态。Windows:下载 Nginx,安装,启动 Nginx Manager,访问默认页面进行验证。

nginx漏洞怎么处理nginx漏洞怎么处理Apr 14, 2025 am 10:21 AM

处理 nginx 漏洞的方法:定期扫描服务器查找漏洞。监控安全公告和更新。检查 nginx 日志以查找异常行为。更新到 nginx 的最新版本,其中包含漏洞修复。应用官方的补丁或安全更新。确保更新已正确应用。保持 nginx 软件和依赖项是最新的。使用安全的配置和最佳实践。定期进行安全扫描和审核。限制对 nginx 的访问和端口。使用防火墙和入侵检测系统。

nginx限流怎么使用nginx限流怎么使用Apr 14, 2025 am 10:18 AM

Nginx 通过以下步骤实施限流:启用限流模块:load_module ngx_http_limit_conn_module.so;配置限流规则:limit_conn_zone $binary_remote_addr zone=mylimit:10m;设置速率限制:limit_conn mylimit 20;拒绝超出限制的请求(默认 503 错误);可选:自定义错误页面:error_page 503 /error-page.html;

怎么解决nginx跨域问题怎么解决nginx跨域问题Apr 14, 2025 am 10:15 AM

解决 Nginx 跨域问题有两种方法:修改跨域响应头:添加指令以允许跨域请求,指定允许的方法和头,以及设置缓存时间。使用 CORS 模块:启用模块并配置 CORS 规则,允许跨域请求、方法、头和设置缓存时间。

nginx伪静态怎么设置nginx伪静态怎么设置Apr 14, 2025 am 10:12 AM

问题:什么是 nginx 伪静态设置?答案:伪静态是通过重写 URL,将动态 URL 转换为静态 URL,从而改善网站的 SEO 和性能。步骤:添加 rewrite 模块;创建伪静态规则;激活 rewrite 模块;重启 nginx。

nginx怎么启动后台包nginx怎么启动后台包Apr 14, 2025 am 10:09 AM

启动 Nginx 后台包需要以下步骤:安装 Nginx检查 Nginx 服务的状态启动 Nginx 服务设置 Nginx 开机自启动配置 Nginx重新加载 Nginx 配置验证 Nginx 是否正在运行

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。