起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2042|回复: 1

[分享] 用nginx做反向代理,让Wex5x的tomcat环境与php共存。

[复制链接]

17

主题

72

帖子

186

积分

初级会员

Rank: 2

积分
186
QQ
发表于 2016-11-29 22:18:49 | 显示全部楼层 |阅读模式
本帖最后由 kacaloyi 于 2016-11-30 10:02 编辑

因为一直用Wex5X开发App,用的是tomcat的环境。
但是PHP下面有很多非常好的东西,希望和Wex5x建立的页面配合使用。

如果这次,我用Wex5X的java环境开发了一个讲故事的应用,但是同时希望有个PHP的博客环境用于官网建设,这样就需要把tomcat与PHP放在一起跑。

研究了两天nginx的反向代理配置。终于把wex5X与PHP在同一个环境中跑起来了。

原理是这样的,wex5有两个模块UI和Baas。
凡是对 x5/ 目录的请求都是要指向UIServer的,凡是对 baas/目录的请求都是要指向BaasServer的。
凡是后缀名是.php的文件,都需要php来处理。
其它的目录和文件,都直接交给nginx处理。

所有的代码,都部署在apache-tomcat\webapps目录中,不管是java的还是php的。
发布程序的时候,用"第一种模式“打包,把打包出来的www目录中的文件拷贝到apache-tomcat\webapps里面部署。

“启动PHP服务.bat” 这个指令,把PHP启动,并绑定9000端口。
“启动WeX5运行平台.bat”这个指令,把Wex5的tomcat环境启动,绑定8080端口。

基于上面的布置,修改wex5/nginx/conf/nginx.conf 。完成修改后
“启动Nginx服务.bat”这个指令,启动nginx,绑定80端口,对外接受请求,并把请求分发给PHP和wex5 .

nginx.conf的修改如下:
=====================我是分割线======================
  1. #user  nobody;
  2. worker_processes  1;

  3. error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;

  6. #pid        logs/nginx.pid;


  7. events {
  8.     worker_connections  64;
  9. }


  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;

  13.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14.     #                  '$status $body_bytes_sent "$http_referer" '
  15.     #                  '"$http_user_agent" "$http_x_forwarded_for"';

  16.     #access_log  logs/access.log  main;

  17.     #autoindex on;
  18.     #autoindex_exact_size on;
  19.     #autoindex_localtime on;

  20.     sendfile        on;
  21.     #tcp_nopush     on;

  22.     #keepalive_timeout  0;
  23.     keepalive_timeout  65;
  24.         
  25.     fastcgi_connect_timeout 300;
  26.     fastcgi_send_timeout 300;
  27.     fastcgi_read_timeout 300;
  28.     fastcgi_buffer_size 128k;
  29.     fastcgi_buffers 4 128k;
  30.     fastcgi_busy_buffers_size 256k;
  31.     fastcgi_temp_file_write_size 256k;

  32.     #gzip  on;
  33.     gzip on;
  34.     gzip_min_length  1k;
  35.     gzip_buffers     4 32k;
  36.     gzip_http_version 1.1;
  37.     gzip_comp_level 2;
  38.     gzip_types       text/plain application/x-javascript text/css application/xml;
  39.     gzip_vary on;
  40.     gzip_disable "MSIE [1-6].";

  41.     server_names_hash_bucket_size 128;
  42.     client_max_body_size     100m;
  43.     client_header_buffer_size 256k;
  44.     large_client_header_buffers 4 256k;

  45.     server {
  46.         listen       80;
  47.         server_name  localhost;

  48.         #charset koi8-r;

  49.         #access_log  logs/host.access.log  main;

  50.                 root   "你的wex5安装目录/apache-tomcat/webapps/";
  51.             #root   "E:/WeX5_V3.5/model/Baas.php/";
  52.                 index  index.php index.html index.htm;
  53.         

  54.         
  55.         location /baas/ {
  56.                 proxy_pass   http://你的服务器IP:8080/baas/;
  57.         #proxy_redirect  off;
  58.         proxy_set_header  X-Real-IP $remote_addr;
  59.         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  60.                         
  61.                 index  index.jsp index.html index.htm;
  62.                 expires      3d;
  63.         }
  64.         
  65.         location /x5/ {
  66.                 proxy_pass   http://你的服务器IP:8080/x5/;
  67.         #proxy_redirect  off;
  68.         proxy_set_header  X-Real-IP $remote_addr;
  69.         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  70.                         
  71.                 index  index.jsp index.html index.htm;
  72.                 expires      3d;
  73.         }

  74.       
  75.       

  76.         #error_page  404              /404.html;

  77.         # redirect server error pages to the static page /50x.html
  78.         #
  79.         error_page   500 502 503 504  /50x.html;
  80.         location = /50x.html {
  81.             root   html;
  82.         }

  83.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  84.         #
  85.         #location ~ \.php$ {
  86.         #    proxy_pass   <a href="http://127.0.0.1" target="_blank">http://127.0.0.1</a>;
  87.         #}

  88.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  89.         
  90.         #location ~ \.php(.*)[        DISCUZ_CODE_0        ]nbsp;
  91.                 location ~ \.php$ {
  92.             fastcgi_pass   127.0.0.1:9000;
  93.             fastcgi_index  index.php;
  94.             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  95.             include        fastcgi_params;
  96.         }

  97.         # deny access to .htaccess files, if Apache's document root
  98.         # concurs with nginx's one
  99.         #
  100.         #location ~ /\.ht {
  101.         #    deny  all;
  102.         #}
  103.     }


  104.    

  105.     # HTTPS server
  106.     #
  107.     #server {
  108.     #    listen       443 ssl;
  109.     #    server_name  localhost;

  110.     #    ssl_certificate      cert.pem;
  111.     #    ssl_certificate_key  cert.key;

  112.     #    ssl_session_cache    shared:SSL:1m;
  113.     #    ssl_session_timeout  5m;

  114.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  115.     #    ssl_prefer_server_ciphers  on;

  116.     #    location / {
  117.     #        root   html;
  118.     #        index  index.html index.htm;
  119.     #    }
  120.     #}

  121. }
复制代码




=====================我是分割线======================
注意替换proxy_pass 指定的IP和root指定的目录。










17

主题

72

帖子

186

积分

初级会员

Rank: 2

积分
186
QQ
 楼主| 发表于 2016-11-30 10:00:25 | 显示全部楼层
本帖最后由 kacaloyi 于 2016-11-30 10:02 编辑

上面的方法还有个漏洞。
PHP是用CGI方式启动的,会随机地自动退出,造成网站没有响应。
所以要改造一下“启动PHP服务.bat”。

指令中用到的RunHiddenConsole 和 xxfpm两个小工具可以在网上下载,然后解压到wex5X的安装目录中,与“启动PHP服务.bat”放在同一个目录中。

================以下是代码==================
  1. @echo off
  2. title 启动PHP服务
  3. @echo 已启动PHP服务...
  4. cd /d %~dp0
  5. set JRE_HOME=""
  6. set JAVA_HOME=""
  7. set CLASSPATH=""
  8. set PATH=%windir%;%windir%\System32;%PATH%
  9. set startDir=%cd%

  10. echo %startDir%|findstr /ibe "[0-9a-z:\\\~\!\@\#\$\(\)\_\+\`\-\=\;\'\.\,]*">nul&&goto run||goto error

  11. :error
  12. echo 错误:不要把版本解压放在包含有中文、空格和特殊字符的目录!
  13. pause
  14. goto end

  15. :run
  16. cd "%startDir%\php"
  17. rem php-cgi.exe -b 127.0.0.1:9000 -c %startDir%\php\php.ini
  18. %startDir%/RunHiddenConsole.exe %startDir%/xxfpm.exe "%startDir%/php/php-cgi.exe -c %startDir%/php/php.ini" -n 2 -i 127.0.0.1 -p 9000

  19. rem 延迟关闭
  20. ping 127.0.0.1 > nul

  21. :end
复制代码


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2025-6-23 16:07 , Processed in 0.055709 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表