解决的核心问题是:使用一个80端口,根据域名、子域名或IP 同时访问java + php运行环境
基本思想:将apache设置为808端口,tomcat设置为8080端口,Nginx设置为80端口,主要是通过Nginx反向代理,通过分别配置的域名,映射到相应的端口对服务器进行访问。
下载Nginx
http://nginx.org/download/nginx-1.16.1.zip
解压后如下
2.nginx的运用
有很多种方法启动nginx
(1)直接双击解压文件夹里的nginx.exe,双击后一个黑色的弹窗一闪而过,证明启动成功
(2)打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start nginx ,回车即可;
常用命令如下:
1. 快速停止或关闭:nginx -s stop
2. 正常停止或关闭:nginx -s quit
3. 配置文件修改重装载命令:nginx -s reload
检查nginx是否启动成功:
cmd命令窗口输入命令:tasklist /fi "imagename eq nginx.exe" 出现如下结果说明启动成功
3.nginx代理设置
①修改要用到的配置文件
..\nginx-1.16.1\conf\nginx.conf
Java
upstream java{
server 127.0.0.1:8080 weight=1;
}
server {
listen 80;
server_name 127.0.0.1;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://java;
proxy_pass_request_headers on;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
PHP
upstream php{
server 127.0.0.1:808 weight=1;
}
server {
listen 80;
server_name www.xuliwei.com;
location / {
proxy_pass http://php;
proxy_pass_request_headers on;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
$_SERVER['HTTP_X_FORWARDED_FOR']就可以拿到真实IP
②Tomcat : ..\apache-tomcat-8.0\conf\server.xml
默认端口为8080
③Apache: ..\Apache24\conf\httpd.conf
Listen 80 改 Listen 808
注意检查下httpd-vhosts.conf下端口是否改成808
④Hosts : C:\Windows\System32\drivers\etc\HOSTS
增加一行数据
127.0.0.1 www.xuliwei.com
重新启动Tomcat/Apache/nginx
最后访问:
127.0.0.1 会进入 8080端口的Tomcat 环境
ww.xuliwei.com会进入 808 端口的 Apache 环境