nodejs App和nginx的服务器配置

时间:2017-09-20 08:13:08

标签: node.js nginx

我有一个应用正在运行 应用程序运行正常。我可以访问根URL。但无法访问其他网址。 App是用nodejs express框架构建的。我的服务器操作系统是Ubuntu 17。 我需要在http://35.202.2.217中运行我的应用程序,而不是在其他端口中运行。 这就是为什么我使用代理通行证。我被困在这里。我该怎么做

例如http://localhost:1336/pages

我正在使用nginx。

我的nginx代码

upstream adshackers {
        server 10.128.0.2:8082;
        server 10.128.0.2:9082;
        server 10.128.0.2:3082;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
        root /var/www/html;
        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;
        server_name adshackers.com;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
        proxy_pass http://adshackers/;
        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;


                try_files $uri $uri/ =404;
        }
        # pass PHP scripts to FastCGI server
        # 
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

1 个答案:

答案 0 :(得分:0)

所以我已经清理了一下并删除了php部分,除非你还提供php文件。

upstream adshackers {
           server localhost:1336;
}

server {
       listen 80 default_server;
       listen [::]:80 default_server;

       root /var/www/html;

       server_name adshackers.com;

       location / {
            proxy_pass http://adshackers;
            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;
       }
}

上游部分必须包含代理对您的应用的请求的信息。我假设你的应用程序托管在你的nginx正在运行的同一台机器上,正在监听localhost:1336,如你的示例所示。

在这种情况下,当http请求到达adshackers.com时(如果DNS条目正确),nginx会将请求转移到localhost:1336

相关问题