如何访问IP+端口号呢?

问答中心分类: Nginx如何访问IP+端口号呢?
linkstar asked 5年 ago
我现在有一个node程序跑在1200端口上,但是访问IP+端口号无法访问,直接输入IP是oneinstack的欢迎页,请问怎么访问呢?
另外我想把这个带端口号的地址绑定到namesilo的二级域名上,请问添加什么记录才可以呢?

5 Answers
oneinstack answered 5年 ago

建议使用nginx反向代理node,可vhost.sh绑定,并修改其中www.example.com为你的域名 8080为你node启动端口,修改:/usr/local/nginx/conf/vhost/www.example.com.conf:

  server {
    listen 80;
    server_name www.example.com example.com;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/path_node_source;
    index index.html index.htm index.jsp;
    #error_page 404 /404.html;
    #error_page 502 /502.html;

    location ~ {
      proxy_pass http://127.0.0.1:8080;
      include proxy.conf;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }

linkstar answered 5年 ago
server {  
    server_name www.xxx.com xxx.com;  
    listen 80;
    listen     443;

          ssl        on;
          ssl_certificate         /xxx/https/domain.pem;
          ssl_certificate_key     /xxx/https/domain.key;

    root /data/wwwroot/www.sxxx.com; 
    index index.html index.htm index.php;

    #rewrite规则
    if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php$1 last;
    }

#node反向代理
location ~ {
      proxy_pass http://127.0.0.1:1200;
      include proxy.conf;
    }

    location /nginx_status {
      stub_status on;
      access_log off;
      #allow 127.0.0.1;
     # deny all;
    }
    location ~ [^/]\.php(/|$) {
    #fastcgi_pass remote_php_ip: 9000;
    #fastcgi_pass 127.0.0.1: 9000; 
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ /\.ht {
      deny all;
    }
    access_log off;

}
我是这样写的配置,只把location加进去了,这样导致Nginx重启失败报错。
Starting Nginx: nginx: [emerg] unknown directive "server" in /usr/local/openresty/nginx/conf/vhost/www.shuxinfe.com.conf:1

这是报错信息,不知道哪写错了  

oneinstack answered 5年 ago
/usr/local/openresty/nginx/conf/vhost/www.shuxinfe.com.conf 这个配置文件有误,nginx -t看下就知道了

linkstar answered 5年 ago
[root@host ~]# nginx -t
nginx: [emerg] unknown directive "server" in /usr/local/openresty/nginx/conf/vhost/www.XXX.com.conf:1
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test failed

报错是第一行,看不出来哪里错了

linkstar answered 5年 ago
问题找到了,我是用了win的记事本编辑保存,加入了BOM,用vscode去掉就好了。
不过加入反向代理这段代码依旧会导致首页无法访问,nginx报错倒是没有。
我用了cloudflare的cdn加速,不知道这个会不会有影响。