Nginx的安装和配置 --解决80端口映射到多个应用服务端口

背景

在微信开发时,填写的网页受权域名不能加上端口号,要直接经过域名访问到应用服务,域名直接访问的默认端口为80, 而我所部署应用的服务器上,80端口已经被httpd进程占用了,由于业务需求,又不能终止httpd使用80端口。因此想到能够经过nginx进行端口映射。html

思路

  • 改变httpd的默认端口为8088,经过nginx把8088端口映射到80端口。
  • 须要访问的网页受权域名(example.com:8080/weixin/pay)把8080端口也映射到80端口。

解决方式

查看80端口占用状况:nginx

netstat -tlnup | grep 80

注意:可能要切换到root权限才能看到运行该端口的进程id和进程名。web

查看httpd的安装目录bash

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服务的启动、中止和重启命令svg

service httpd start
service httpd stop
service httpd restart

修改httpd默认端口url

vi /etc/httpd/conf/httpd.conf

Listen 80 改成: listen 8088spa

安装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来访问部署的应用服务