技术解析

折腾一晚vps,给nginx做域名反向代理失败,请教如何正确设置?
0
1975-02-22 05:07:14
idczone
想把jiyin.it这个域名跟放在GAE上的网站绑定,在vps上用LNMP装了nginx,在nginx.conf中添加了

server
{
listen 80;
server_name jiyin.it;

location / {
proxy_pass http://geekiwi.appspot.com/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

jiyin.it的A记录也已经指向了vps的IP地址199.15.116.122。

但是访问jiyin.it却还是一个PHP探针页面,原先以为是hosts的问题,所以在vps的hosts中加入了:

203.208.45.206 geekiwi.appspot.com

但是还是不行,删掉这段之后访问,却大带宽服务器显示502 Bad Gateway。

请教如何设置才能生效?
502 Bad Gateway
nginx/0.7.61

因为我刚删掉了vps的hosts中这段:
203.208.45.206 geekiwi.appspot.com
然后就502了。。

自检没有报错?

刚才代码里没有}闭合,但是加上}闭合之后,还是不行
[[email&www]/usr/local/nginx/sbin/nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
依然跳转到探针页面。
贴出我的nginx.conf文件:
user www www;
worker_processes 1;
error_log /home/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name bod.fm;
index index.html index.htm index.php;
root /home/www;
crawler 20;
/status {
/> /> location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /home/logs/access.log access;
}
server
{
listen 80;
server_name jiyin.it;
location / {
proxy_pass http://geekiwi.appspot.com/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server
{
listen 80;
server_name yagbodu.com;
location / {
proxy_pass http://erwenit.appspot.com/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

server {
listen 81;
server_name jiyin.it;
location / {
proxy_redirect off;
proxy_pass http://geekiwi.appspot.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

端口设置为每个server_name都不同?

不是,我只是测试,80也可以

自己vps上测试了可以

jiyin.it 现在代理设置是OK的~~,不过点用户链接是404,还未完成的站点么?
先许个愿:妹子~~~~~

本地测试,jiyin.it指向127.0.0.1,反代成功:

vps上jiyin.it应指向vps的IP
只是在http {} 两括号之间加上了:
http://gist.github.com/2132139.js

我改动了一下conf文件,然后新加上了你的。结果访问jiyin.it变成一个nginx的欢迎页面了。。。悲催。。。
——————————nginx.conf文件:——————————
user www www;
worker_processes 2;
error_log /home/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http {
include mime.types;
default_type application/octet-stream;
main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
logs/access.log main;
sendfile on;
on;
0;
keepalive_timeout 65;
on;
server {
listen 80;
server_name localhost;
koi8-r;
logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
404 /404.html;
redirect server error pages to the static page /50x.html
/> error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
proxy the PHP scripts to Apache listening on 127.0.0.1:80
/> ~ \.php$ {
proxy_pass http://127.0.0.1;
pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
/> ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
deny access to .htaccess files, if Apache's document root
concurs with nginx's one
/> ~ /\.ht {
deny all;
/> }
server {
listen 81;
server_name jiyin.it;
location / {
proxy_redirect off;
proxy_pass http://geekiwi.appspot.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

现在变成 Welcome to nginx!了

“vps上jiyin.it应指向vps的IP ”
——
请问你是说的hosts?还是nginx.conf里的proxy_pass项?

listen 81; 换成 80,我刚才想改81,但是不小心就提交了,
修改之前你现在访问 jiyin.it:81是没有问题的

server {
listen 80;
server_name jiyin.it;
location / {
proxy_redirect off;
proxy_pass http://geekiwi.appspot.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
这样就OK了,
如果listen 81就得用 http://jiyin.it:81/ 来访问了

nginx.conf里就是gist的代码那样了,我是本地测试才把127.0.0.1 jiyin.it写到hosts里的,你把jiyin.it指向199.15.116.122已经可以了。

server {
listen 80;
server_name www.jiyin.it;
access_log /var/log/nginx/localhost.access.log;
location / {
proxy_pass http://ghs.google.com;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
root /var/www/nginx-default;
index index.html index.htm;
}
}

把listen 81改成了listen 80后还是欢迎页面,会不会是nginx.conf里第一个server对第二个server有干扰?
重新帖一遍现在的nginx.conf的内容:
——————————nginx.conf文件:——————————
user www www;
worker_processes 2;
error_log /home/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http {
include mime.types;
default_type application/octet-stream;
main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
logs/access.log main;
sendfile on;
on;
0;
keepalive_timeout 65;
on;
server {
listen 80;
server_name localhost;
koi8-r;
logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
404 /404.html;
redirect server error pages to the static page /50x.html
/> error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
proxy the PHP scripts to Apache listening on 127.0.0.1:80
/> ~ \.php$ {
proxy_pass http://127.0.0.1;
pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
/> ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
deny access to .htaccess files, if Apache's document root
concurs with nginx's one
/> ~ /\.ht {
deny all;
/> }
server {
listen 80;
server_name jiyin.it;
location / {
proxy_redirect off;
proxy_pass http://geekiwi.appspot.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

现在OK的,CTRL+F5看看?

可以看到基因了,你浏览器多刷新几次,或者关掉重开,或者换一个

新加的access_log是干嘛的?
[emerg]: open() "/var/log/nginx/localhost.access.log" failed (2: No such file or directory)

你们那边都可以直接用jiyin.it访问吗?我这边怎么还不行。。。。纠结。。

是的,能直接访问
CTRL+F5,
或者清空你本地的DNS缓存看看

多谢大家的帮忙,我刚换了台电脑能访问了。
应该是我自己那台电脑的hosts文件有问题,以前加过google的hosts。
再次感谢!

提交表单后的跳转貌似有点问题:
点“说出来”后,chrome左下角显示”正在向geekiwi.appspot.com提交数据“,然后chrome地址栏就变成了geekiwi.appspot.com,于是显示被墙。再进入jiyin.it可以看到刚发布的内容

^_^ 要不要上个LUManager试试呀 http://www.v2ex.com/t/29571

我也遇到这问题,但是不知道什么原因。。。
我用的是Cyberduck,还挺好用的。

LUManager在vps管理上很全面,web服务器就是apache+nginx+tengine,1.1.9免费版就已经很强悍了,设置个反向代理什么的不在话下。
最近开始走商业化的2.0.x版更加面面俱到,都可以拿出来直接做idc了,个人使用更加游刃有余,欢迎了解哦。

不用那么多吧~~~
server
{
listen 80;
server_name www.aaa.com;
location / {
proxy_pass http://www.bbbb.com/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

访问了一下,可以正常访问啊

麻烦您不要在技术讨论帖里面发这种广告了,好好的讨论串里面突然给你插一个这样的帖子

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服