upstream cluster{
server 127.0.0.1:3333;
server 127.0.0.1:4444;
server 127.0.0.1:5555;
}
server {
listen 1111;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://cluster;
}
这是nginx.conf中我修改的地方,原先nginx是好的,自从我加了这个upstream后就404了,但是upstream里的转发地址都在浏览器中测试过可以访问,那到底是啥问题啊,访问localhost:1111显示404,访问localhost:3333,localhost:4444,localhost:5555都是可以正常访问的
3333,4444,5555都是什么服务?
我本地没有这些端口的服务,但我如果稍微改一下你的配置,把这三个端口也起在nginx上,是可以访问的。
所以问题可能不在nginx配置,你想想有没有什么可能导致nginx无法连同3333,4444,5555这三个端口的服务?
确认一下nginx正常启动没有,看看服务状态和nginx的日志
贴一个最简单能跑的配置吧,你可以参考一下
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
upstream cluster {
server 127.0.0.1:3333;
server 127.0.0.1:4444;
}
server {
listen 1111;
server_name localhost;
location / {
proxy_pass http://cluster;
}
}
server {
listen 3333;
server_name localhost;
return 200 '{"msg":"3333","code":0,"data":""}';
}
server {
listen 4444;
server_name localhost;
return 200 '{"msg":"4444","code":0,"data":""}';
}
}
listen 直接接听80端口,直接访问localhost试试
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream cluster{
server 127.0.0.1:3333;
server 127.0.0.1:4444;
server 127.0.0.1:5555;
}
server {
listen 1111;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://cluster;
}
#error_page 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
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.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
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}