dongsuoxi1790 2013-07-08 20:02
浏览 61
已采纳

NGINX:domain + subdomain上的node.js + php

I'm trying to set node.js app on main domain and php-based forum on subdomain. Node.JS app works on 8000 port. Here's my config:

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

}

Both node.js app & php forum can't be reached at myawesomeapp.ru. 127.0.0.1:8000 shows nodejs-app. What's wrong with my config? Thanks.

p.s. my php files are placed in /usr/share/nginx/html

  • 写回答

2条回答 默认 最新

  • douzhe3516 2013-07-09 09:09
    关注

    Please include any messages you see on response of trying to visit both vhosts. As well make sure you include this setup in your nginx config as well as service nginx reload after changing configurations.

    In order to proxy nginx to node you have to use upstreams. Here is configuration that might suit your needs:

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

    For your forum try this config:

    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;
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊