banner

[Rule] Rules  [Home] Main Forum  [Portal] Portal  
[Members] Member Listing  [Statistics] Statistics  [Search] Search  [Reading Room] Reading Room 
[Register] Register  
[Login] Loginhttp  | https  ]
 
Forum Index Thảo luận hệ điều hành *nix Lỗi 502 Bad Gateway nginx  XML
  [Question]   Lỗi 502 Bad Gateway nginx 18/02/2014 10:00:14 (+0700) | #1 | 279805
[Avatar]
Framer
Member

[Minus]    0    [Plus]
Joined: 06/12/2012 19:30:36
Messages: 6
Offline
[Profile] [PM]
Hi all,
Hiện tại mình đang triển khai Nginx làm Reversed Proxy cho Apache.
Mô hình của mình thế này: Client -> Nginx (IP: 172.20.20.237) -> Apache (IP: 172.20.20.238)
-> Đã kiểm tra cấu hình thì thấy báo OK nhưng khi mình chạy thì lại không được.
1. Kiểm tra cấu hình Nginx
[root@Nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful 

2. Selinux và Iptables:
[root@Nginx ~]# cat /etc/sysconfig/selinux

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted  

[root@Nginx ~]# service iptables status
iptables: Firewall is not running.  

3. Cấu hình nginx.conf
[root@Nginx ~]# cat /etc/nginx/nginx.conf
user nobody;
worker_processes 4;
error_log logs/error.log crit;

worker_rlimit_nofile 8192;

events {
worker_connections 1024; # you might need to increase this setting for busy servers
use epoll; # Linux kernels 2.6.x change to epoll
}

http {
server_names_hash_max_size 2048;
server_names_hash_bucket_size 512;

server_tokens off;

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

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10;

# Gzip on
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain application/x-javascript text/xml text/css;

# Other configurations
ignore_invalid_headers on;
client_max_body_size 8m;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;

# Cache most accessed static files
open_file_cache max=10000 inactive=10m;
open_file_cache_valid 2m;
open_file_cache_min_uses 1;
open_file_cache_errors on;

# virtual hosts includes
include "/etc/nginx/conf.d/*.conf";

}  

4. File Proxy
[root@Nginx ~]# cat /etc/nginx/conf.d/hienhai.com.conf
server {
listen 172.20.20.237:80;
server_name hienhai.com hienhai.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|fl v|mp3|mid|js|css|html|htm|wml)$ {
root /var/www/html/hienhai.com;
expires 365d;
}

location / {
client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_connect_timeout 30s;

proxy_wwwect http://www.hienhai.com:8080 http://www.hienhai.com;
proxy_wwwect http://hienhai.com:8080 http://hienhai.com;

proxy_pass http://172.20.20.238:8080/;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}  

5. Trên Apache:
Đã thêm Virtualhost:
NameVirtualHost 172.20.20.238:8080

# Define Server document root
DocumentRoot /var/www/html/

# Define the virtual host
<VirtualHost 172.20.20.238:8080>
ServerName www.hienhai.com
ServerAlias hienhai.com
DocumentRoot /var/www/html/hienhai.com
<Directory "/var/www/html/hienhai.com">
Options FollowSymLinks -Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
</VirtualHost>  

- Đã chỉnh sửa :
Listen 8080  

- Đã cài đặt module:mod_rpaf với cấu hình như sau:
<IfModule mod_rpaf.c>
RPAF_Enable On
RPAF_ProxyIPs 127.0.0.1 172.20.20.237
RPAF_Header X-Forwarded-For
RPAF_SetHostName On
RPAF_SetHTTPS On
RPAF_SetPort On
</IfModule>  

- Đã Disabled Selinux và Stop Iptables.
6. Kết quả:
Khi vào web thì hiển thị thông báo lỗi:
502 Bad Gateway
nginx  

- Log Apache không có gì.
- Log Nginx
[root@Nginx conf.d]# tail -f /var/log/nginx/access.log
172.20.20.138 - - [16/Feb/2014:09:37:58 +0700] "GET / HTTP/1.1" 502 166 "-" "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0"
172.20.20.138 - - [16/Feb/2014:09:37:59 +0700] "GET /favicon.ico HTTP/1.1" 404 162 "-" "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0"
172.20.20.138 - - [16/Feb/2014:09:37:59 +0700] "GET /favicon.ico HTTP/1.1" 404 162 "-" "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0"
172.20.20.138 - - [16/Feb/2014:09:43:27 +0700] "GET / HTTP/1.1" 502 166 "-" "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0"
172.20.20.138 - - [16/Feb/2014:09:43:32 +0700] "GET / HTTP/1.1" 502 166 "-" "Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0" 
Keep calm and be a duck
[Up] [Print Copy]
  [Question]   Lỗi 502 Bad Gateway nginx 18/02/2014 21:26:54 (+0700) | #2 | 279813
tuanksor
Member

[Minus]    0    [Plus]
Joined: 01/11/2011 02:44:03
Messages: 50
Offline
[Profile] [PM]
Bạn kiểm tra lại xem apache có đang hoạt động bình thường không (truy cập trực tiếp ko qua proxy), kiểm tra iptables tại server apache đã accept port 8080 chưa.
[Up] [Print Copy]
  [Question]   Lỗi 502 Bad Gateway nginx 19/02/2014 09:54:06 (+0700) | #3 | 279816
[Avatar]
Framer
Member

[Minus]    0    [Plus]
Joined: 06/12/2012 19:30:36
Messages: 6
Offline
[Profile] [PM]

tuanksor wrote:
Bạn kiểm tra lại xem apache có đang hoạt động bình thường không (truy cập trực tiếp ko qua proxy), kiểm tra iptables tại server apache đã accept port 8080 chưa. 

Hi tuanksor:
- Trước khi cấu hình Nginx thì mình đã test thử Apache chạy Port 8080 OK.
- Info mình đưa ra mình cũng đã nói là Iptables đã Stop luôn rồi
Keep calm and be a duck
[Up] [Print Copy]
  [Question]   Lỗi 502 Bad Gateway nginx 19/02/2014 23:10:56 (+0700) | #4 | 279821
tuanksor
Member

[Minus]    0    [Plus]
Joined: 01/11/2011 02:44:03
Messages: 50
Offline
[Profile] [PM]
Lỗi 502 thường do frontend (nginx as proxy) nhận đc invalid respone từ backend (apache) (có thể apache ko chạy, hay bị drop bởi iptables). Bạn kiểm tra truy cập từ nginx -> apache xem thế nào
Code:
#curl -IL http://172.20.20.238:8080
[Up] [Print Copy]
[digg] [delicious] [google] [yahoo] [technorati] [reddit] [stumbleupon]
Go to: 
 Users currently in here 
1 Anonymous

Powered by JForum - Extended by HVAOnline
 hvaonline.net  |  hvaforum.net  |  hvazone.net  |  hvanews.net  |  vnhacker.org
1999 - 2013 © v2012|0504|218|