2013-03-11 87 views
14

我試圖運行一個最低限度的反向代理,並想出了以下內容:nginx的簡單proxy_pass到本地主機不工作

events { 
    worker_connections 4096; 
} 

http { 
    server { 
     listen 80; 
     location/{ 
      proxy_pass http://127.0.0.1:3000/; 
     } 
    } 
} 

`

然而,當我訪問這臺服務器,我得到標準的「歡迎到nginx頁面」,而不是從端口3000上運行的服務器的響應。

如果我ssh到機器並運行curl http://127.0.0.1:3000/,我得到所需的結果(並最終我在端口上運行該服務器80它工作得很好,所以我kn它必須處理反向代理配置)。

+0

相同的配置工作正常,在我的devbox。似乎你的網絡服務器的網絡有問題。你有沒有嘗試重新啓動你的網絡? – 2013-03-11 13:27:23

+0

你確定你沒有在其他conf文件中的其他代理指令? proxy.conf? – Pixou 2013-03-11 16:57:51

+0

是否刪除尾部斜線做任何事情? – Bart 2013-03-14 22:54:40

回答

22

我有完全相同的問題。我只是註釋掉一條線在我的nginx.conf文件:

 
include /etc/nginx/sites-enabled/*; 

通過回答改爲

 
#include /etc/nginx/sites-enabled/*; 
+9

這工作對我來說,謝謝。它確實禁用了什麼? – bengem 2014-06-29 11:37:31

+0

你說什麼不行。我沒有'/ etc/nginx/sites-enabled/*;'在'/ etc/nginx'文件夾中的任何地方發表評論或配置仍然存在並且遇到問題 – Green 2017-02-24 05:25:00

+0

@Green有點遲了,但是碰巧這個問題我自己。在我的情況下,包含文件是'/etc/nginx/conf.d/ *。conf' - 這包括與我的站點配置衝突的默認站點的配置。在你的情況下,它可能是代理配置本身的問題 – codemonkey 2017-03-17 17:53:38

4

Explainng維傑的帖子,因爲交換不會讓我評論。

註釋啓用網站的目錄可能是必需的,因爲您使用的是標準nginx.conf文件。您會注意到該行已經在http指令中。如果您使用的是標準配置,則您將在另一個http指令中重新定義http指令。你也可以更新你的站點文件,只有服務器指令而不是http指令。

標準上下的nginx.conf文件:內

worker_processes 4; 

error_log /var/log/nginx/error.log; 
pid  /var/run/nginx.pid; 

events { 
    worker_connections 1024; 
} 

http { 

    include  /etc/nginx/mime.types; 
    default_type application/octet-stream; 

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

    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 

    keepalive_timeout 65; 

    gzip on; 
    gzip_http_version 1.0; 
    gzip_comp_level 5; 
    gzip_proxied any; 
    gzip_vary off; 
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml; 
    gzip_min_length 1000; 
    gzip_disable  "MSIE [1-6]\."; 

    server_names_hash_bucket_size 64; 
    types_hash_max_size 2048; 
    types_hash_bucket_size 64; 
    client_max_body_size 1024; 

    include /etc/nginx/conf.d/*.conf; 
    include /etc/nginx/sites-enabled/*; 
} 

例如compatiable站點文件站點啓用-:

server { 
    server_name {server name}; 
    listen 80; 
    access_log /var/log/nginx/access.log; 
    error_log /var/log/nginx/error.log; 
    location/{ 
     proxy_pass http://example.com:8080; 
     proxy_set_header Host $host; 
    } 
} 
+0

你說什麼不行。我在nginx文件夾的任何地方沒有任何'include/etc/nginx/sites-enabled/*;'並且遇到問題 – Green 2017-02-24 05:24:04

-1

檢查從鏈接的網站,啓用站點可用可能有所幫助。 我的符號鏈接指向無處,因此nginx從未讀取配置。 在我的情況下,它是

ls -lh /etc/nginx/sites-enabled 
lrwxrwxrwx 1 root root 23 Feb 19 11:11 default -> sites-available/default 

,而不是

ls -lh /etc/nginx/sites-enabled 
lrwxrwxrwx 1 root root 23 Feb 19 11:11 default -> ../sites-available/default