背景
在微信开发时,填写的网页授权域名不能加上端口号,要直接通过域名访问到应用服务,域名直接访问的默认端口为80, 而我所部署应用的服务器上,80端口已经被httpd进程占用了,因为业务需求,又不能终止httpd使用80端口。所以想到可以通过nginx进行端口映射。
思路
- 改变httpd的默认端口为8088,通过nginx把8088端口映射到80端口。
- 需要访问的网页授权域名(example.com:8080/weixin/pay)把8080端口也映射到80端口。
解决方式
查看80端口占用情况:
netstat -tlnup | grep 80
注意:可能要切换到root权限才能看到运行该端口的进程id和进程名。
查看httpd的安装目录
find / -name 'httpd'
输出为
/var/log/httpd
/var/cache/httpd
/run/httpd
/etc/httpd
/etc/logrotate.d/httpd
/etc/sysconfig/httpd
/usr/lib64/httpd
/usr/share/httpd
/usr/include/httpd
/usr/libexec/initscripts/legacy-actions/httpd
/usr/sbin/httpd
进入httpd目录
cd /etc/httpd
ls
输出为:
conf conf.d conf.modules.d logs modules run
httpd服务的启动、停止和重启命令
service httpd start
service httpd stop
service httpd restart
修改httpd默认端口
vi /etc/httpd/conf/httpd.conf
Listen 80 改为: listen 8088
安装nginx
先安装PCRE pcre-devel 和Zlib
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
安装nginx
wget -c https://nginx.org/download/nginx-1.14.0.tar.gz
解压并进入nginx目录
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
使用nginx的默认配置
./configure
编译安装
make
make install
查找安装路径
whereis nginx
进入/usr/local/nginx/sbin目录启动nginx
cd /usr/local/nginx/sbin
./nginx
nginx重启命令
cd /usr/local/nginx/sbin
./nginx -s reload
查看是否启动成功
ps -ef|grep nginx
输出为:
root 21724 1 0 Oct24 ? 00:00:00 nginx: master process ./nginx
nobody 21725 21724 0 Oct24 ? 00:00:00 nginx: worker process
root 24062 24044 0 09:14 pts/0 00:00:00 grep --color=auto nginx
可以通过kill命令终止进程来结束nginx运行
kill 21724
配置nginx端口映射
vi /usr/local/nginx/conf/nginx.conf
配置文件增加内容如下
server {
listen 80;
server_name localhost ;
index productor.html orderSure.html;
location /bj62/ { #httpd服务映射资源
proxy_pass http://127.0.0.1:8088;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_buffers 32 4k;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
}
location / { #部署项目映射资源
proxy_pass http://127.0.0.1:8080; # 网页授权域名要访问的应用url
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_buffers 32 4k;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
}
}
至此可以验证:
直接通过example.com/weixin/pay来访问部署的应用服务
版权声明:本文为weixin_43398820原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。