2016-06-08 43 views
0

我有一个作为Web服务器由Nginx控制的Web应用程序。当它被配置如下,它的工作。打到http://192.168.33.10:9174/产生“需要402付款”Nginx可以作为反向代理,但不会通过套接字将流量传递给Puma

# /etc/nginx/sites-enabled/myapp 

server { 
    listen 9174; 
    server_name 127.0.0.1; 
    location /status { 
    stub_status on; 
    access_log off; 
    } 
    return 402; 
} 

它的工作原理。

然后我试图让它将流量转发到Puma应用服务器,但失败了。然后我设置了一堆代理如下:

# /etc/nginx/sites-enabled/myapp 
upstream sqlitething { 
    server www.sqlite.org; 
} 

upstream hwacithing { 
    server www.hwaci.com; 
} 

upstream appthing { 
    #server unix:///home/deployer/myapp/shared/sockets/socktest.sock 
    server unix:/home/deployer/myapp/shared/sockets/puma.sock fail_timeout=0; 
} 

server { 
    listen 80; 
    server_name 192.168.33.10; 
    location/{ 
    proxy_pass http://sqlitething; 
    } 
} 

server { 
    listen 8080; 
    server_name 192.168.33.10; 
    location/{ 
    proxy_pass http://hwacithing; 
    } 
} 

server { 
    listen 8099; 
    server_name 192.168.33.10; 
    location/{ 
    proxy_set_header X-Forwarded-For $remote_addr; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 
    proxy_pass http://appthing; 
    } 
} 

server { 
    listen 9175; 
    server_name 192.168.33.10; 
    return 402; 
} 

这里是我的结果时,我打了浏览器的网址:

套接字文件存在与权限如下:

srwxrwxrwx 1 deployer deployer 0 Jun 8 19:15 /home/deployer/myapp/shared/sockets/puma.sock 

-------- UPDATE 6月8日5: 57 EST --------------------

我尝试了失败的网址如下:

$ curl -isL http://192.168.33.10:8099 
HTTP/1.1 301 Moved Permanently 
Server: nginx/1.1.19 
Date: Wed, 08 Jun 2016 21:52:54 GMT 
Content-Type: text/html 
Transfer-Encoding: chunked 
Connection: keep-alive 
Location: https://appthing/ 
Vary: Accept-Encoding 

我试成功URL以下

$ curl -isL http://192.168.33.10:8080 
HTTP/1.1 200 OK 
Server: nginx/1.1.19 
Date: Wed, 08 Jun 2016 21:54:46 GMT 
Content-Type: text/html 
Content-Length: 1167 
Connection: keep-alive 
Vary: Host 
Last-Modified: Wed, 13 Dec 2006 14:54:32 GMT 
ETag: "88047-48f-4247d938f5a00" 
Accept-Ranges: bytes 

<html><head><title>Hwaci Homepage</title></head><body bgcolor="white"> 
    <font size="7"><b>Hwaci</b></font><br> 



...etc... 

----- UPDATE 6月9日上午11时24分EST --------------

我在线加入

proxy_set_header X-Forwarded-For $remote_addr; 
proxy_set_header Host $http_host; 
proxy_redirect off; 

到连接到Unix套接字的端口8099的服务器/监听块。我得到了同样的结果。

我后来去http://blog.honeybadger.io/how-unicorn-talks-to-nginx-an-introduction-to-unix-sockets-in-ruby/当他们指定成立了一个小插座服务器,但与

unix:///home/deployer/myapp/shared/sockets/socktest.sock 

插座然后我修改了nginx.conf所以它不会守护进程和错误会去到标准输出。

daemon off; 
error_log /dev/stdout info; 

然后我nginx的跑了作为

> sudo nginx -c nginx.conf 

当我跑到卷曲击中的nginx /插座组合,我得到这个:

$ curl -isL http://192.168.33.10:8099 
HTTP/1.1 502 Bad Gateway 
Server: nginx/1.1.19 
Date: Thu, 09 Jun 2016 15:13:57 GMT 
Content-Type: text/html 
Content-Length: 173 
Connection: keep-alive 

<html> 
<head><title>502 Bad Gateway</title></head> 
<body bgcolor="white"> 
<center><h1>502 Bad Gateway</h1></center> 
<hr><center>nginx/1.1.19</center> 
</body> 
</html> 

nginx的日志是这样的:

2016/06/09 15:13:34 [notice] 9885#0: start worker process 9889 
2016/06/09 15:13:49 [info] 9886#0: *1 client 192.168.33.1 closed keepalive connection 
2016/06/09 15:13:57 [crit] 9886#0: *3 connect() to unix:/home/deployer/myapp/shared/sockets/socktest.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.33.1, server: 192.168.33.10, request: "GET/HTTP/1.1", upstream: "http://unix:/home/deployer/myapp/shared/sockets/socktest.sock:/", host: "192.168.33.10:8099" 
2016/06/09 15:13:57 [info] 9886#0: *3 client 192.168.33.1 closed keepalive connection 

----- 6月9日结束更新--------- ---------- - - - - - - - ---

彪马进程正在运行

[email protected]:/etc/nginx/sites-enabled$ ps -ef | grep puma 
deployer 21319  1 0 19:15 ?  00:00:00 puma 2.16.0 (unix:///home/deployer/myapp/shared/sockets/puma.sock) [20160608191112]                                                                                  
deployer 21322 21319 0 19:15 ?  00:00:09 puma: cluster worker 0: 21319 [20160608191112]                                                                                            
deployer 21326 21319 0 19:15 ?  00:00:10 puma: cluster worker 1: 21319 [20160608191112] 

我配置所述上游彪马作为各种配置指南指定。我究竟做错了什么?

有没有办法查看Puma进程是否真正在为我的Web应用程序提供服务?

有没有办法使用curl或wget和通过Unix套接字来访问Puma进程(unix:/home/deployer/myapp/shared/sockets/puma.sock)?

+0

你是什么意思的“试图重定向,但没有发生”?当你点击'http://192.168.33.10:8099 /'url时,究竟发生了什么? – Roman

+0

网络浏览器将我重定向到https:// appthing。 –

+0

你的意思是说,'https:// appthing /'字面意思? – Roman

回答

0

最终,我得到了socket服务器通过做

> chmod 777 /home/deployer/myapp/shared/sockets/socktest.sock 

这告诉我,我的Nginx设置好工作。

但是Puma套接字一直没有通过。

原来彪马应用服务器后面的Web应用程序被配置为:

config.force_ssl = true 

一旦我意识到,我配置的Nginx为侦听端口443,开启SSL,设置一些自签名证书,和.........它的工作。

所有这一切的教训是,我对Nginx,Puma,套接字,权限和SSL的了解同时都很弱,这就是为什么我无法弄清楚什么是错误的原因。

1

在我看来,你的美洲狮应用程序正试图从http升级到https连接。它通过发出重定向来做到这一点。为了保持便携式应用程序,它使用请求头中的HOST字段来确定其名称。

在您的位置块中,您没有指定任何proxy_set_header指令,因此nginx已插入HOST和CONNECTION的默认值。 HOST的价值是“appthing”。

你可能需要设置一些自己的proxy_set_header指令让应用程序知道其真实名称,可能说服应用程序,连接已经是安全的,并可能设置一些proxy_redirect指令,以虚假的重定向映射到一个可接受的值。

有关详细信息,请参阅this document