2016-06-08 46 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