阿里云lmnp环境根据url配置端口转发可同时使用80端口访问php、java项目

需求背景

正式服务器环境安装有一套lmnp环境,运行着discuz程序,有域名。现需要通过一套java语言的springboot项目定时修改discuz库中的数据。由于调用了微信公众号的接口,必须使用80端口。但是已被php程序占用,所以需要使用nginx的转发功能进行配置。

功能处理

开始是先在本地进行测试,由于是mac系统,本身已有apache、php的运行环境。java是自己的老本行,相关工具当然健在。在安装一个nginx环境即可。可见《MAC系统中运行mysql+apache+php环境,方便快速搭建discuz,dedecms,wordpress环境等》 也就是本地搭建了一套lmap环境 + java项目 + nginx的配置。

php端口为8081,java端口为8080,nginx监听80端口,根据url转发配置及说明如下:

= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~*  开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。


server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        proxy_pass http://localhost:8081;
    }

    location /wx/ {
        proxy_pass http://localhost:8080;
    }
}

当访问localhost的时候,两个服务都可以正常访问。以为放到线上就可以了,但是不行。线上的php是用nginx启用了fastcgi相关配置来运行的,本身没有使用apache服务器。
默认配置如下,可见配置文件读取/alidata/server/nginx/conf/vhosts/*.conf目录下的

error_log  /alidata/log/nginx/error.log crit;
pid        /alidata/server/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}


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

        #charset  gb2312;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 8m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;

        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-javascript text/css application/xml;
        gzip_vary on;
        #limit_zone  crawler  $binary_remote_addr  10m;
        log_format '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


        include /alidata/server/nginx/conf/vhosts/*.conf;
}

conf相关文件配置如下

upstream tomcat {
        server  127.0.0.1:8080;
}

#server {
#        listen  80;
#        server_name     xxx.com;

#        location /lawwx/ {
#                proxy_pass http://tomcat;
#        }
#}

server {
        listen       80;
        server_name  xxx.com;
        #index index.html index.htm index.php;
        root /alidata/www/;

        location / {
                index index.html index.htm index.php;
                root /alidata/www/;
        }

        location /lawwx/
        {
                root /home/;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_redirect off;
                proxy_pass http://127.0.0.1:8080;
        }

        location ~ \.(php|php5)?$
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ \.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ \.(js|css)?$
        {
                expires 1h;
        }

        include /alidata/server/nginx/conf/rewrite/discuz_x3.conf;
        access_log  /alidata/log/nginx/access/ottffss.log;
        error_page 404  /404.html; #
}

根据上面的配置,可以看到根据url路径做了对应的转发以及资源的缓存。但是每次访问的时候只能匹配到php环境。java路径始终无法匹配。把其中php相关的注释掉,就可以访问java环境功能。(应该是什么配置的不对?还是这种配置是错误的,网上几个这种的环境配置,但是拿来用依然无法使用)。如果我在安装一个apache服务器去运行php项目,在用nginx转发apache和java项目,就和本地的配置一样了。肯定可以成功。但是服务器资源紧张,不能安装其他软件了。

这里利用了二级域名处理了这个问题。www.xxx.com用来处理php业务, wx.xxx.com用来处理java业务。

做了二级域名后的配置如下

upstream tomcat {
        server  127.0.0.1:8080;
}

server {
        listen  80;
        server_name     wx.xxx.com;

        location /lawwx/ {
                proxy_pass http://tomcat;
        }
}

这样终于可以了用80端口同时访问 java、php项目了。
有经验的人,可以解答下根据url配置为啥不生效

你可能感兴趣的:(问题解惑)