最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】nginx配置正确但是却始终无法实现二级域名的端口转发效果

配置 crifan 26浏览 0评论
折腾:
【未解决】阿里云中二级域名转发中如何实现端口转发
期间,在确保nginx配置正确的前提下,去看看二级域名的端口转发效果。
但是始终不对:
先去访问url,看看能否转发
http://xxx:33800/storybook?q=apple
换成:
http://dev.xxx/storybook?q=apple
结果:
http://dev.xxx/storybook?q=apple
->说明至少配置文件是生效了?
只是配置规则不对?
去看看access的log
[root@xxx nginx]# ll
total 220
-rw-r--r-- 1 root root    500 Nov 19 16:00 
dev.xxx.conf
-rw------- 1 root root   1370 Mar 15  2018 phpfpm_status.conf
-rw-r--r-- 1 root root    544 Mar 15  2018 phpinfo.conf
-rw-r--r-- 1 root root   1206 Mar 15  2018 
www.xxx.conf
-rw-r--r-- 1 root root 207476 Nov 19 16:19 ystemctl status firewalld.service
[root@xxx nginx]# cat /www/wwwlogs/
dev.xxx.log
 
[root@xxx nginx]# ll /www/wwwlogs/
total 10264
-rw-r--r-- 1 root root        0 Jan 27  2018 access.log
-rw-r--r-- 1 root root        0 Nov 19 16:07 
dev.xxx.log
-rw-r--r-- 1 root root      895 Nov 19 16:10 nginx_error.log
drwxr--r-- 2 www  www      4096 Jan 27  2018 waf
-rw-r--r-- 1 root root 10495074 Nov 19 16:24 
www.xxx.log
[root@xxx nginx]# tail /www/wwwlogs/
www.xxx.log
 
222.92.130.218 - - [19/Nov/2018:16:06:32 +0800] "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 162 "-" "Safari%20%E6%B5%8F%E8%A7%88%E5%99%A8/14606.1.36.1.9 CFNetwork/974.1 Darwin/18.0.0 (x86_64)"
222.92.130.218 - - [19/Nov/2018:16:06:32 +0800] "GET /apple-touch-icon.png HTTP/1.1" 404 162 "-" "Safari%20%E6%B5%8F%E8%A7%88%E5%99%A8/14606.1.36.1.9 CFNetwork/974.1 Darwin/18.0.0 (x86_64)"
222.92.130.218 - - [19/Nov/2018:16:06:32 +0800] "GET /favicon.ico HTTP/1.1" 404 162 "-" "Safari%20%E6%B5%8F%E8%A7%88%E5%99%A8/14606.1.36.1.9 CFNetwork/974.1 Darwin/18.0.0 (x86_64)"
222.92.130.218 - - [19/Nov/2018:16:06:32 +0800] "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 162 "-" "Safari%20%E6%B5%8F%E8%A7%88%E5%99%A8/14606.1.36.1.9 CFNetwork/974.1 Darwin/18.0.0 (x86_64)"
222.92.130.218 - - [19/Nov/2018:16:06:32 +0800] "GET /apple-touch-icon.png HTTP/1.1" 404 162 "-" "Safari%20%E6%B5%8F%E8%A7%88%E5%99%A8/14606.1.36.1.9 CFNetwork/974.1 Darwin/18.0.0 (x86_64)"
222.92.130.218 - - [19/Nov/2018:16:06:32 +0800] "GET /favicon.ico HTTP/1.1" 404 162 "-" "Safari%20%E6%B5%8F%E8%A7%88%E5%99%A8/14606.1.36.1.9 CFNetwork/974.1 Darwin/18.0.0 (x86_64)"
61.129.6.147 - - [19/Nov/2018:16:08:27 +0800] "GET / HTTP/1.1" 200 20222 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
66.249.66.59 - - [19/Nov/2018:16:12:31 +0800] "GET /robots.txt HTTP/1.1" 404 162 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +
http://www.google.com/bot.html
)"
66.249.66.61 - - [19/Nov/2018:16:12:32 +0800] "GET / HTTP/1.1" 200 5268 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +
http://www.google.com/bot.html
)"
87.251.81.86 - - [19/Nov/2018:16:24:00 +0800] "GET /admin/assets/js/views/login.js HTTP/1.1" 404 162 "-" "python-requests/2.19.1"
[root@xxx nginx]# tail /www/wwwlogs/nginx_error.log 
2018/03/27 11:39:50 [crit] 1228#0: *1 connect() to unix:/tmp/php-cgi-56.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /phpfpm_56_status HTTP/1.1", upstream: "
fastcgi://unix:/tmp/php-cgi-56.sock
:", host: "127.0.0.1"
2018/03/27 11:39:50 [crit] 1228#0: *3 connect() to unix:/tmp/php-cgi-56.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /phpfpm_56_status HTTP/1.1", upstream: "
fastcgi://unix:/tmp/php-cgi-56.sock
:", host: "127.0.0.1"
2018/11/19 16:10:15 [alert] 1237#0: worker process 1241 exited on signal 9
2018/11/19 16:10:19 [alert] 1237#0: worker process 1243 exited on signal 9
2018/11/19 16:10:21 [alert] 1237#0: cache manager process 1244 exited on signal 9
2018/11/19 16:10:47 [alert] 1237#0: worker process 4385 exited on signal 9
[root@xxx nginx]# 
没有发现log啊。
想起来了,搞错了,应该换成:
http://dev.xxx/storybook/storybook?q=apple
结果错误依旧:
但是希望有log日志啊
[root@xxx nginx]# ll /www/wwwlogs/
total 10264
-rw-r--r-- 1 root root        0 Jan 27  2018 access.log
-rw-r--r-- 1 root root        0 Nov 19 16:07 
dev.xxx.log
-rw-r--r-- 1 root root      895 Nov 19 16:10 nginx_error.log
drwxr--r-- 2 www  www      4096 Jan 27  2018 waf
-rw-r--r-- 1 root root 10495385 Nov 19 16:33 
www.xxx.log
发现www.xxx.log是有log的
去看看
[root@xxx nginx]# tail /www/wwwlogs/
www.xxx.log
 
66.249.66.48 - - [19/Nov/2018:16:33:16 +0800] "GET /robots.txt HTTP/1.1" 404 162 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +
http://www.google.com/bot.html
)"
66.249.66.48 - - [19/Nov/2018:16:33:16 +0800] "GET / HTTP/1.1" 200 5268 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +
http://www.google.com/bot.html
)"
换个Chrome访问,问题依旧:
看配置:
感觉是:
www.xxx.conf
的配置所捕获了,没有传递给
dev.xxx.conf
那去试试:
删除:dev.xxx.conf
dev.xxx.conf的内容,放到www.xxx.conf的后面:
[root@xxx nginx]# rm -f 
dev.xxx.conf
 
[root@xxx nginx]# rm 
www.xxx.conf
 
rm: remove regular file ‘
www.xxx.conf
’? y
[root@xxx nginx]# rz
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring 
www.xxx.conf
...
  100%       1 KB       1 KB/sec    00:00:01       0 Errors  

[root@xxx nginx]# ll
total 12
-rw------- 1 root root 1370 Mar 15  2018 phpfpm_status.conf
-rw-r--r-- 1 root root  544 Mar 15  2018 phpinfo.conf
-rw-r--r-- 1 root root 1708 Nov 19 16:38 
www.xxx.conf
[root@xxx nginx]# cat 
www.xxx.conf
 
server
{
    listen 80 default_server;
    server_name 
www.xxx;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/
www.xxx;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-56.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/
www.xxx.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        access_log off; 
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        access_log off; 
    }
    access_log  /www/wwwlogs/
www.xxx.log;
}

server
{
    listen 80;
    server_name 
dev.xxx;

    access_log  /www/wwwlogs/
dev.xxx.log;

    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;

    location /storybook/
    {
        # proxy_redirect off;

        # proxy_pass 
http://xxx:33800;
        proxy_pass 
http://xxx:33800/;
    }

}[root@xxx nginx]# service nginx stop
Stoping nginx...  done
[root@xxx nginx]# service nginx start
Starting nginx...  done
[root@xxx nginx]# service nginx status
nginx (pid 4784 4783 4782 4781 4780) already running.
看看是否有效:
结果无效。
www.xxx.log连新log都没有
把server换到上面部分试试
然后发现:
http://dev.xxx
还是可以打开首页的:
但是:
http://dev.xxx/storybook/storybook?q=apple
http://dev.xxx/storybook/
http://xxx:33800
始终都无法打开
然后去掉了dev.xxx的server配置后,现象和上面一样
-》说明上面的
的跳转是DNS域名转发得到的
而不是此处转发前的服务器的nginx的配置生效的。
然后突然想到:
貌似是搞错服务器了?
应该是去配置目标服务器中的nginx才对啊。。。
nginx反向代理二级域名及修改默认端口-毛虫小臭臭-51CTO博客
“例如我要把子域名 jira.51cto.com 绑定到 服务器的 8001端口,我首先要保证访问 jira.51cto.com 的请求能到达该服务器,然后Nginx才能处理。”
-》很明显是:
此处应该去配置,转发后的,目标服务器中,去配置nginx才对。。。
然后后续去正确的服务器中,添加nginx配置,就可以了。
【总结】
此处之前是被某些帖子的解释(不够清楚)所误导,以为:
增加nginx的配置,是针对,被转发之前的服务器的
此处是:
www.xxx的带域名的服务器
(但是当访问:
dev.xxx,被转发到:
dev服务器:xxx的33800端口上)
而实际上是:
应该对于,被转发过去的,目标服务器:
此处是:dev服务器 xxx
中去更新nginx配置才对
-》这时也才更加深刻理解:
被转发到的,目标服务器中的server中的:
server
{
    listen 80;
    server_name dev.xxx;
指的是:
目标服务器中,判断首先是80端口的访问,且(服务器的名字?)来自于 dev.xxx
然后才触发转发。

转载请注明:在路上 » 【已解决】nginx配置正确但是却始终无法实现二级域名的端口转发效果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
24 queries in 0.082 seconds, using 9.49MB memory