nginx服务器路由问题(404)

时间:2014-12-10 17:53:22

标签: nginx routing config

我使用nginx来托管多个节点应用程序。我正在努力将localhost:80链接到我运行的永久应用程序。

我有多个应用程序最初的工作方式如下,我只是去每个端口:

____________      ___________     
|           |    |    my     |
| localhost | -->| running   |
|           |    |  app(s)   |
|  (port #) |    |           |
|___________|    |___________|   

但是由于我们在不同的端口上有大约4个应用程序,我们希望它能够像这样工作:

____________      ___________     ____________
|           |    |           |   |    my      |
|  nginx    | -->| proxy     |-->|  running   |
|localhost80|    | pass      |   |   app(s)   |
|___________|    |___________|   |____________|

nginx应该路由到一个页面但是我得到了#34; 404 Not Found"这意味着nginx正在运行,但它不知道它在寻找什么。

以下是我尝试运行的一个示例服务器应用程序的代码:

它是一个正在侦听某个端口的永久应用程序(.js文件)。

#Server proxy for haiyan_dashboard
server {
      listen        80; #
      server_name     localhost/haiyan;

    location / {
      proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

}

此外,这是我的整个配置文件:

user  nobody;
worker_processes  5;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
worker_connections  1024;
}


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;

server {
listen      80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
       #root   html;
         #root /var/www/webviz.redcross.org/public_html;
       proxy_pass https://webviz.redcross.org/services/tables;
       #index  index.html index.htm;
    }

    #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;
    #}
}
#Server proxy for haiyan_dashboard
server {
      listen        8080; #
      server_name     localhosthaiyan;

    location / {
      proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

}
#Proxy for Haiti_Baseline
server {
    listen          3001;
    server_name     localhaiti_baseline;

    location / {
    proxy_pass http://localhost:3001/secure/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
}
    #ServerProxy for RFL Border
server {
    listen          80; #is this also 3002???
    server_name     rfl_border;

    location / {
    proxy_pass http://localhost:3002/secure/; #Change the URL!!!
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
}
    #ServerProxy for Basic Borders
server {
    listen          80;
    server_name     basic_border;

    location / {
    proxy_pass http://localhost:9001/secure/; #This URL is Different
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       80;
#    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;
#    }
#}

}

我是否正确使用代理通行证?如果没有,它如何以最蹩脚的方式运作。此外,所有4个服务器代码都位于nginx.conf文件中。这也是一种很好的做法。

(注意:我已经阅读了nginx网站上的文档)

如果您需要我提供更多说明,我们很乐意添加它。

1 个答案:

答案 0 :(得分:1)

以下是解决方案:

server {
      listen      80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    # Default Route
    location / {
    root html;
    }

    # Route to haiyan
    location /haiyan_dashboard {
       #root   html;
         #root /var/www/webviz.redcross.org/public_html;
       #proxy_pass https://webviz.redcross.org/services/tables;
       proxy_pass http://localhost:8080/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host $host;
       proxy_cache_bypass $http_upgrade;
       #index  index.html index.htm;
    }

    # Route to Haiti Baseline
    location /Haiti_baseline {
    proxy_pass http://localhost:3001/secure/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

    # Route to RFL_Border
    location /RFL_border-master {
    proxy_pass http://localhost:3002/secure/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

    # Route to Basic Borders
    location /basic-borders {
    proxy_pass http://localhost:9001/secure/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

    #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;
    #}
}

`