分类 服务器 下的文章

背景

  1. 家里宽带有公网IP,可以在外访问家里电脑、群晖等服务。
  2. 但是家里公网IP不是固定的,每次重启路由器会自动更换。
  3. 家里服务太多、且IP不好记,需要通过域名访问。

解决方案

注册购买一个域名、或使用免费域名。将家里的公网IP解析到该域名下。通过域名进行访问家里电脑、群晖图片和视频服务等。每次路由器重启、断网等原因导致Ip更换后,将新IP更新到域名解析中。
为了保证每次宽带IP更换后,及时更新解析到域名。需要在路由器上添加一个脚本去自动更新。我们将脚本设置为每分钟执行一次(根据自己需要设置),如果IP地址有变化,就将新IP解析到域名。为了方便更新,dns服务商支持api更新时必须的,而DNSPod完美支持,还免费,就选择使用它了。关于如何将域名使用dnspod解析请自行百度。

步骤

理论可行,下面进入实施阶段。

DNSPod配置

DNSPod上查看API_ID,并申请API_Token。具体过程很简单,自行百度即可。

编辑并上传脚本

将脚本中的API_IDAPI_Token替换为你自己在dnspod上申请的值,将domain替换为你的域名,host替换为你要解析的二级域名即可。脚本如下:

#!/bin/sh
#CONF START
API_ID=110000
API_Token=0dcfxxxxxxxxx6b6
domain=abc.com
host=k2p
CHECKURL="http://ip.03k.org"
#OUT="pppoe"
#CONF END
. /etc/profile
date
if (echo $CHECKURL |grep -q "://");then
IPREX='([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
URLIP=$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s $CHECKURL|grep -Eo "$IPREX"|tail -n1)
if (echo $URLIP |grep -qEvo "$IPREX");then
URLIP="Get $DOMAIN URLIP Failed."
fi
echo "[URL IP]:$URLIP"
dnscmd="nslookup";type nslookup >/dev/null 2>&1||dnscmd="ping -c1"
DNSTEST=$($dnscmd $host.$domain)
if [ "$?" != 0 ]&&[ "$dnscmd" == "nslookup" ]||(echo $DNSTEST |grep -qEvo "$IPREX");then
DNSIP="Get $host.$domain DNS Failed."
else DNSIP=$(echo $DNSTEST|grep -Eo "$IPREX"|tail -n1)
fi
echo "[DNS IP]:$DNSIP"
if [ "$DNSIP" == "$URLIP" ];then
echo "IP SAME IN DNS,SKIP UPDATE."
exit
fi
fi
token="login_token=${API_ID},${API_Token}&format=json&lang=en&error_on_empty=yes&domain=${domain}&sub_domain=${host}"
Record="$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s -X POST https://dnsapi.cn/Record.List -d "${token}")"
iferr="$(echo ${Record#*code}|cut -d'"' -f3)"
if [ "$iferr" == "1" ];then
record_ip=$(echo ${Record#*value}|cut -d'"' -f3)
echo "[API IP]:$record_ip"
if [ "$record_ip" == "$URLIP" ];then
echo "IP SAME IN API,SKIP UPDATE."
exit
fi
record_id=$(echo ${Record#*\"records\"\:\[\{\"id\"}|cut -d'"' -f2)
record_line_id=$(echo ${Record#*line_id}|cut -d'"' -f3)
echo Start DDNS update...
ddns="$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s -X POST https://dnsapi.cn/Record.Ddns -d "${token}&record_id=${record_id}&record_line_id=${record_line_id}")"
ddns_result="$(echo ${ddns#*message\"}|cut -d'"' -f2)"
echo -n "DDNS upadte result:$ddns_result "
echo $ddns|grep -Eo "$IPREX"|tail -n1
else echo -n Get $host.$domain error :
echo $(echo ${Record#*message\"})|cut -d'"' -f2
fi

编辑完成后,将脚本上传路由器。每个路由器不同,自行查阅如何开启ssh以及其默认密码后自行上传。

配置定时任务

路由器一般都有定时任务功能。在定时任务功能下添加新任务。以K2p为例,在功能设置-高级设置中,设置计划任务(定时任务),因我将执行脚本上传到了root目录下,每分钟进行一次监测,所以定时任务脚本为:

*/1 * * * * /root/dnspod_ddns.sh &> /dev/null

图片

保存后,等待一会,查看dnspod管理后台中,域名解析是否已经自动更新。

配置端口转发

这样域名已经解析到了你的带宽公网IP,想要访问电脑、内网服务等,再进行一次端口转发设置,就可以通过域名愉快的访问自己的电脑或其他服务了。

图片

背景

公司部门内,配置了内网使用的域名。服务器部署完成后,nginx配置时,使用了该域名。请求后发现抛出了502 Bad Gateway

原因

查看nginx日志,发现了如下信息

2022/03/14 09:04:19 [error] 27#27: *41 no resolver defined to resolve ***.***.com, client: 10.10.60.155, server: ***.***.com, request: "POST /demo/captcha/anon/getLoginCaptcha HTTP/1.1", host: "***.***.com", referrer: "http://***.***.com/login/login"

很明显,是因为nginx无法解析该域名导致了。我们只需要让nginx能识别该域名即可。

解决方案

解决方法很简单,只需要在nginx全局配置(nginx.conf)的http项内,添加对应的dns解析即可resolver dns服务器地址;。如下所示:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    resolver 10.10.10.1; #配置dns地址
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

背景

为了方便,使用了docker部署了wordpress,通过nginx进行反向代理,并配置了域名,安装完成一切正常,但是在上传主题是出现了如下错误提示:

413 Request Entity Too Large

百度google一通后,在http{}中加入 client_max_body_size 10m;解决了。

然后上传文件,确出现了另一个错误提示:

The uploaded file exceeds the upload_max_filesize directive in php.ini.

解决方案

很明显,这是php上的限制,只需要修改php.in即可。
为了方便修改,我们在创建容器时,添加volumes将php配置映射到本地,完整的docker-compose.yml如下:

version: '3'
services:
  wordpress:
    image: wordpress:latest
    container_name: wordpress
    restart: always
    ports:
      - "9102:80"
    volumes:
      - ./html:/var/www/html
      - ./php:/usr/local/etc/php
    environment:
      WORDPRESS_DB_HOST: 10.0.4.5:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: 123456
      WORDPRESS_DB_NAME: wp_demo

启动容器后,将php文件夹中的php.ini-production文件拷贝一份更名为:php.ini,编辑该文件,修改其中的upload_max_filesize项目的值,如下:

upload_max_filesize = 20M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

重启容器,即可解决问题。

背景

在云服务器上搭建了wordpress,为了方便部署,采用了docker方式,使用了9102的端口号。然后通过nginx进行反向代理,使用域名访问。

问题

nginx配置很简单,直接使用了proxy_pass配置到docker容器的wordpress地址,如下:

server {
    listen 80;
    server_name www.我的域名.com;
    location / {
        proxy_pass http://10.0.4.6:9102;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_redirect off;
    }
}

配置完成后,访问域名进行安装,配置,都正常。管理后台也可正常访问。但是想要进入博客首页时,出了问题,直接跳转到了http://10.0.4.6:9102。很明显,这个地址是无法访问也是不对的。
总结下问题:域名+/其他页面的形式是可以正常访问的,但是如果只有域名来访问首页就会出现301重定向问题。

Request URL: http://我的域名.com/
Request Method: GET
Status Code: 301 Moved Permanently (from disk cache)
Remote Address: 127.0.0.1:8889
Referrer Policy: strict-origin-when-cross-origin

Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Thu, 19 Aug 2021 06:42:17 GMT
Location: http://10.0.4.5/
Server: nginx/1.21.1
X-Powered-By: PHP/7.4.22
X-Redirect-By: WordPress

解决方案:

多方查找,找到了方案,安装如下配置进行设置,即可解决。

server {
    listen       80;
    server_name  我的域名.com;
    
    set $node_port 9102;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://10.0.4.5:$node_port$request_uri;
        proxy_redirect off;
    }
}