【问题标题】:How do I set up my nginx files to connect to my server?如何设置我的 nginx 文件以连接到我的服务器?
【发布时间】:2020-11-16 08:07:30
【问题描述】:

我有一个 MySQL 数据库和 React 前端,它们在开发中使用 Node 服务器连接两者。我有一个刚刚创建的 VPS,并且正在使用 Nginx 来控制集成。不幸的是,我是新手,当它在盒子上运行时无法连接到节点服务器。我知道它正在侦听正确的端口,因为netstat -tulpn 显示它是(见下文)。

我可以从我的笔记本电脑通过 SSH 直接连接到 MySQL 数据库,所以我知道它工作正常。就像我说的,前端加载但无法连接到服务器登录(见下文)。

我怀疑我的 Nginx 配置出错了,但在过去五个小时尝试了各种想法后,我不知所措。

Nginx 默认配置文件

server {
    listen         443 default_server ssl;
    #listen        [::]:443 default_server ssl;
    server_name    thesystem.co.za;

    #ssl_certificate /etc/letsencrypt/live/thesystem.co.za/fullchain.pem;
    #ssl_certificate_key /etc/letsencrypt/live/thesystem.co.za/privkey.pem;

    expires       0;
    add_header    Cache-Control  public;
    add_header    Cache-Control  no-store;
    add_header    Cache-Control  no-cache;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options nosniff;
    add_header 'Referrer-Policy' 'origin';
    add_header X-XSS-Protection "1; mode=block";
    add_header Set-Cookie "HTTPOnly; HttpOnly; Secure";

    location / {
        root /usr/share/nginx/html/thesystem;
    }

    location /integration/ {
       charset_types application/json;
       charset UTF-8;
       proxy_pass http://localhost:8081/api/;
    }
}

如果有人能够就我做错了什么提供一些见解,我将不胜感激。我有一个测试客户端在几个小时后开始,但我被困在最后一个障碍!

**** 更新 ****** 如果我在登录到盒子时直接使用 curl 访问服务器,我会收到响应,所以我知道它正在监听。就像我说的那样,我一定是在 nginx 配置上做错了,但我不知道是什么......

**** 包含 nginx.conf nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    #large_client_header_buffers 16 64k;
    #client_max_body_size 100M;

    # Added By Willie for security
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers On;
    ssl_ciphers ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS;
    # Added By Willie, hide Nginx version
    server_tokens off;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip on;
    gzip_disable "msie6";


    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/rss+xml
        image/svg+xml;

    include /etc/nginx/conf.d/*.conf;
}

*** 第二次更新*** 我已将default.conf 减少到最低限度,它不会重定向到网页或服务器的正确位置。我得到的只是一个 404 页面。


server {
    listen         80 default_server;
    server_name    thesystem.co.za;


    location / {
        root /usr/share/nginx/html/thesystem;
    }

    location /integration/ {
       charset_types application/json;
       charset UTF-8;
       proxy_pass http://localhost:8081/;
       proxy_set_header Proxy "";
    }
}

【问题讨论】:

  • 您在 http 标头中使用了charset_types 吗?是 nginx 还是您的 nodejs 应用程序返回 404?在location /integration 中放置一些静态且独立于 URL 的内容作为测试。
  • 是的,我在标题中使用了charset_types,它设置为application/json。我不确定如何在那里放置静态的东西,但会尝试。

标签: mysql node.js nginx routes


【解决方案1】:

您的本地 api 路径是什么?是不是像http://localhost:8081/api/admin/session

通常是因为错误的proxy_pass 路由配置。有时它有或没有斜线问题。有时别名不正确...

更改``proxy_pass```, 然后就可以访问了

https://thesystem.co.za/integration/api/admin/sessions/ 

nginx.conf:

    location /integration/ {
       #....
       proxy_pass http://localhost:8081/;
    }

【讨论】:

  • 是的,没错。如果我在服务器上运行curl http://localhost:8081/api/admin/clients,我会得到响应。我的数据库因抱怨需要创建一个目录而崩溃,但这是另一个问题。
  • 很遗憾,问题依然存在
  • 我已将 nginx.conf 文件添加到我的原始帖子中以供参考。错误日志` 2020/07/27 03:28:49 [错误] 1496#0: *5 在 SSL 握手时侦听 SSL 端口的服务器中未定义“ssl_certificate”,客户端:196.49.19.32,服务器:0.0.0.0: 443 2020/07/27 03:43:44 [错误] 1496#0: *6 在 SSL 握手时侦听 SSL 端口的服务器中未定义“ssl_certificate”,客户端:51.178.78.153,服务器:0.0.0.0:443 2020 /07/27 05:04:04 [notice] 2847#0:信号进程已启动 2020/07/27 05:13:02 [notice] 2922#0:信号进程已启动 `
  • 我的意思是error.log,通常在 nginx 的根目录下,它可以帮助您确定本地服务中哪个确切的路由是 404。
  • 对不起,我以为那是我为您发布的内容。这些都是我在nginx目录下的所有文件conf.d fastcgi_params mime.types scgi_params win-utf default.d fastcgi_params.default mime.types.default scgi_params.default fastcgi.conf koi-utf nginx.conf uwsgi_params fastcgi.conf。 default koi-win nginx.conf.default uwsgi_params.default
【解决方案2】:

事实证明我的 nginx 配置没有问题。我的主机提供商没有正确更新 DNS 记录,所以它指向的是网站而不是 VPS!

如果其他人遇到同样的问题,请在此处检查以确保 Remote Address 是正确的。我所做的只是将它复制并粘贴到我的浏览器中,当我最终到达其他地方时,我知道出了点问题。

感谢大家的帮助,尤其是@dunhuang

【讨论】:

    猜你喜欢
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多