使用80端口绑定相同域名的不同后缀

我买的是阿里云服务器自带了wordpress ,但同时直接占用了80端口

80端口是不需要显性添加,可以直接输入 http://127.0.0.1 == http://127.0.0.1:80

但我还想上传别的程序,就只能用其它端口了,很不方便!

所以今天正好尝试下利用服务器

首先现在服务器增加两个端口 8080 8081

然后就可以进xftp配置文件了,这里找到nginx的conf配置文件

加入配置代码
server {
    listen       8080;  #使用的端口
    server_name  localhost;#域名
    root   /var/www/cg/;#文件目录
    location / {#后缀
        try_files $uri $uri/ /index.php?$query_string;
       index  index.php index.html index.htm;#默认访问页面
    }
}
8081端口也一样

这里我们需要重启服务器更新配置才能看到 如http://101.132.189.142:8080/

然后比较难的是我们的wordpress不能访问了!

这里需要进我们数据库

更改下 option_value的 siteurl'和home值

# wp_options表 查询记录
select * from wp_options where option_name in ('siteurl','home');
 
# wp_options表 更新option_value的值
update wp_options set option_value='http://www.example.com:8081' where option_name in ('siteurl','home');

还要替换下 post_content

UPDATE wp_posts SET post_content = REPLACE( post_content, "http://www.example.com", "http://www.example.com:8001" ) where 1;

这里都是简单的MySQL语句就不解释放图了

我们的8081端口的wordpress就可以访问了 如 https://blog.rick.icu/

最后一步 就是配置80端口绑定不同的后缀了

server {
    listen       80;
    server_name  localhost;
     root  var/www/; #配置本地html页面,可有但没用
    index  index.html index.htm;
    location /article/ #后缀{
			proxy_pass http://127.0.0.1:8081/;
        }
    location /cg/#后缀{
			proxy_pass http://127.0.0.1:8080/;
        }
}
 

http://127.0.0.1/cg就等于 http://127.0.0.1:8080/;

http://127.0.0.1/ article 就等于 http://127.0.0.1:8081/;

通过以上方式只要服务器性能足够,想绑定多少个服务就多少个了。

总结

80端口比较安全,不需要显性添加的

同时不仅可以使别人能简单访问到多个页面 http://101.132.189.142/cg http://101.132.189.142/article

还可以用来可以解决微信公众号等平台只能绑定80端口问题!

解决cors 跨域问题 (21.8.17号更新)

所在端口 location 加入

 location / {
       add_header Access-Control-Allow-Origin *;   加入此行
       try_files $uri $uri/ /index.php?$query_string;
        index  index.php index.html index.htm;
    }