Nginx来代理Apache

我正在尝试将Nginx设置为Apache的反向代理服务器,用于在本地运行的站点:8080,并通过lessico.pistacchioso.com外部访问。 当前的configuration导致502 – 错误的网关错误。

  #/etc/apache2/ports.conf Listen 127.0.0.1:8080 <IfModule mod_ssl.c> # If you add NameVirtualHost *:443 here, you will also have to change # the VirtualHost statement in /etc/apache2/sites-available/default-ssl # to <VirtualHost *:443> # Server Name Indication for SSL named virtual hosts is currently not # supported by MSIE on Windows XP. Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule> 

然后

  #/etc/apache2/sites-enabled/lessico NameVirtualHost 127.0.0.1:8080 <VirtualHost 127.0.0.1:8080> ServerAdmin webmaster@localhost ServerName lessico.pistacchioso.com DocumentRoot /home/pistacchio/sites/lessico/ <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /home/pistacchio/sites/lessico/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog /var/log/apache2/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost> 

最后

  #/etc/nginx/sites-enabled/default server { listen 80; server_name corpus.pistacchioso.com; location / { proxy_pass http://127.0.0.1:9000/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_max_temp_file_size 0; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } } server { listen 80; server_name lemmi.pistacchioso.com; access_log /var/log/nginx/localhost.access.log; location / { proxy_pass http://127.0.0.1:8080/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_max_temp_file_size 0; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } 

}

任何帮助如何正确设置这个? 谢谢

通常,502表示NGINX服务器无法连接到您的上游代理。

服务器充当网关或代理,并收到来自上游服务器的无效响应

-Wikipedia

如果您查看错误日志(可能是/var/log/nginx/error.log,/ var / log / nginx / error_log或/usr/local/nginx/var/log/error.log中的一个,但查看您的configuration)你应该会看到一些相关的错误。

 2012/02/03 00:00:00 [alert] 31291#0: *240 round robin upstream stuck on 2 tries while connecting to upstream, client: 1.2.3.4, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "example.com" 

如果你看到类似的东西,这可能意味着你的NGINX服务器无法与上游进行通话。 如果你SSH连接到服务器,你可以运行几个命令来检查一个到上游的基本连接是否可以build立。 尝试运行命令;

 curl -I http://127.0.0.1:8080/ 

这将HTTP HEAD请求发送到您的本地服务器。 在输出中,第一行应该是类似的

 HTTP/1.1 200 OK 

要么

 HTTP/1.0 200 OK 

如果它不是200响应代码,那意味着Apache服务器有问题。 如果这是一个500+错误检查Apache错误日志,看看他们的问题是什么。 如果这个命令给你任何types的超时或类似的错误

 curl: (7) couldn't connect to host 

Apache服务器存在networking问题。

首先尝试检查你的防火墙,可能会阻塞端口。 我想通常所有的防火墙都应该允许环回地址上的所有端口,但我可能是错的,所以总是值得检查。 跑

 iptables --list | grep 8080 

这将返回任何与端口8080相关的防火墙规则,但不会确认是否阻塞或解除阻塞,但会标记任何明显的规则。 接下来检查Apache是​​否正在运行,并侦听它认为正在侦听的端口。

 ps aux | grep httpd 

这将返回所有的Apache进程,至less应该返回两个结果(一个httpd进程和一个grep httpd进程),但可能会有更多,具体取决于您的configuration。 接下来检查端口运行这个

 lsof -i :8080 

这将返回在端口9000上监听的所有进程。至less应该有一个“httpd”进程,看起来像这样

 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME httpd 25353 apache 3u IPv6 98385 TCP *:http (LISTEN) 

这是一个相当不错的诊断,“是一个正确的进程”,如果你的防火墙没问题,进程已经启动,并在正确的端口上侦听,但是你仍然无法得到任何回应,甚至是一个错误,问题可能会很多更深。 发布你的Apache和NGINX错误日志的最后几行,他们可能会提供更多的信息。