【问题标题】:NGINX: node.js + php on domain + subdomainNGINX:域+子域上的node.js + php
【发布时间】:2013-07-08 20:02:43
【问题描述】:

我正在尝试在主域上设置 node.js 应用程序,在子域上设置基于 php 的论坛。 Node.JS 应用程序适用于 8000 端口。这是我的配置:

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;

    server_name myawesomeapp.ru;

    location / {
        proxy_pass http://127.0.0.1:8000;
        access_log off; 
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
        root /srv/myawesomeapp/static;
    }
}

server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        server_name forum.myawesomeapp.ru

        location / {
                try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

}

在 myawesomeapp.ru 上无法访问 node.js 应用程序和 php 论坛。 127.0.0.1:8000 显示 nodejs-app。我的配置有什么问题?谢谢。

附言我的php文件放在/usr/share/nginx/html

【问题讨论】:

    标签: php node.js nginx subdomain config


    【解决方案1】:

    请包括您在尝试访问两个虚拟主机时看到的任何消息。还要确保在您的 nginx 配置中包含此设置以及更改配置后的service nginx reload

    为了将 nginx 代理到节点,您必须使用上游。以下是可能适合您需要的配置:

    upstream node {
      server 127.0.0.1:8000;
    }
    
    server {
      listen 80;
    
      server_name myawesomeapp.ru;
    
      location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
        access_log off;
        root /srv/myawesomeapp/static
        try_files $uri $uri/ =404;
        expires 365d;
      }
    
      location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
    
        proxy_pass http://node/;
        proxy_redirect off;
      }
    }
    

    对于您的论坛,试试这个配置:

    server {
      server_name www.forum.myawesomeapp.ru;
      rewrite ^(.*) http://forum.myawesomeapp.ru$1 permanent;
    }
    
    server {
      listen 80 default_server;
      server_name forum.myawesomeapp.ru;
    
      root /usr/share/nginx/html;
      index index.php;
      charset utf-8;
    
      gzip on;
      gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    
      location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
        access_log off;
        try_files $uri $uri/ =404;
        expires 365d;
      }
    
      error_page 404 /404.html;
    
      location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_intercept_errors on;
      }
    }
    

    【讨论】:

    • 谢谢,但是 forum.myapp.com 仍然显示节点应用程序。我尝试了fastcgi_pass 127.0.0.1:9000;fastcgi_pass unix:/var/run/php5-fpm.sock 选项(使用/etc/php5/fpm/pool.d/www.conf 监听选项编辑)
    • service nginx reload 显示没有错误,sudo service php5-fpm restart 也显示。
    • 确保您已正确包含配置。
    【解决方案2】:

    尝试从您的配置中删除这些行:

    listen [::]:80 default_server ipv6only=on;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 2016-06-17
      • 2011-08-21
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多