运行Apache2 Server和Node Server [NGINX]

时间:2015-09-03 05:01:04

标签: node.js apache nginx

我有一台安装了几个node.Js app的服务器。我按照本指南设置了反向代理设置:https://www.digitalocean.com/community/tutorials/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab

所以我的/etc/nginx/conf.d文件夹中挤满了Nginx配置文件,例如:

server {
  listen 80;

  server_name my-domain.com;

  location / {
    proxy_pass http://localhost:3001;  // my node app runs on port 3001
    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;
  }
}

然后,我安装并设法在端口3002上运行我的apache2服务器。我尝试使用此配置:

server {
  listen 80;

  server_name my-wordpress-blog.com;

  location / {
    proxy_pass http://localhost:3002/wordpress-folder-name; // yes, this is for wordpress 
    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;
  }
}

问题是:每当我尝试访问my-wordpress-blog.com时,都会将我重新路由到my-wordpress-blog.com/html并出现404错误:The requested URL /htmlhtml/ was not found on this server..那么如何正确设置?谢谢

1 个答案:

答案 0 :(得分:0)

当您为我自己的wordpress安装请求我的配置脚本时。它基于Debian提供的默认nginx和php5-fpm包。不确定它是否是最好的配置,因为它的旧,但它的工作原理;)

/etc/nginx/sites-enabled/my-site.conf

# Upstream to abstract backend connection(s) for php
upstream php {
        server unix:/tmp/php-cgi.socket;
        #server 127.0.0.1:9000;
}
server {
        ## Your website name goes here.
        server_name example.com;
        ## Your only path reference.
        root /var/www/your-wp-installation;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content
                try_files $uri $uri/ /index.php;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

我还包括fastcgi.conf文件,但我认为它是默认的

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;