简体   繁体   English

如何设置我的 nginx 文件以连接到我的服务器?

[英]How do I set up my nginx files to connect to my server?

I have a MySQL database and React front end that work in development using a Node server to connect the two.我有一个 MySQL 数据库和 React 前端,它们在开发中使用节点服务器连接两者。 I have a VPS that I just created and am using Nginx to control the integration.我有一个刚刚创建的 VPS,并且正在使用 Nginx 来控制集成。 Unfortunately, I am new to this and can't connect to the Node server when it's running on the box.不幸的是,我是新手,当它在盒子上运行时无法连接到节点服务器。 I know it's listening on the correct port because netstat -tulpn shows it is (see below).我知道它正在侦听正确的端口,因为netstat -tulpn显示它是(见下文)。

服务器端口

I can connect directly to the MySQL database via SSH from my laptop so I know that's working properly.我可以从我的笔记本电脑通过 SSH 直接连接到 MySQL 数据库,所以我知道它工作正常。 Like I said, the front end loads but can't connect to the server to log in (see below).就像我说的,前端加载但无法连接到服务器登录(见下文)。

404错误

I suspect I have made an error with my Nginx configuration but after trying various ideas for the past five hours, I'm at a loss.我怀疑我的 Nginx 配置出错了,但在过去五个小时尝试了各种想法后,我不知所措。

Nginx default.conf Nginx default.conf

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

If anyone is able to offer some insight as to what I'm doing wrong, I would really appreciate.如果有人能够就我做错了什么提供一些见解,我将不胜感激。 I have a test client starting in a few hours and I'm stuck at the last hurdle!我有一个测试客户端在几个小时后开始,我被困在最后一个障碍!

**** UPDATE ****** If I hit the server directly using curl when I'm logged on to the box, I get a response so I know it is listening. **** 更新 ****** 如果我在登录到盒子时直接使用 curl 访问服务器,我会收到响应,所以我知道它正在监听。 I must be doing something wrong with the nginx config like I said but I don't know what...就像我说的那样,我必须对 nginx 配置做错了,但我不知道是什么......

**** Included nginx.conf nginx.conf **** 包括 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;
}

*** Second update *** I've reduced the default.conf to the bare minimum and it doesn't redirect to the proper location for the webpage or the server. *** 第二次更新 *** 我已将default.conf减少到最低限度,它不会重定向到网页或服务器的正确位置。 All I get served is a 404 page.我得到的只是一个 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 "";
    }
}

what's your local api path?你当地的 api 路径是什么? is it like http://localhost:8081/api/admin/session ?是不是像http://localhost:8081/api/admin/session

Usually it is because of the wrong proxy_pass route config.通常是因为错误的proxy_pass路由配置。 sometimes it is with or without slash problem.有时它有或没有斜线问题。 sometimes the alias name is not correct...有时别名不正确...

Change the ``proxy_pass```, then you can access to更改“proxy_pass”,然后就可以访问了

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

nginx.conf: nginx.conf:

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

It turns out there was nothing wrong with my nginx configuration.事实证明,我的 nginx 配置没有任何问题。 My host provider hadn't updated the DNS records properly so it was pointing to the website instead of the VPS!我的主机提供商没有正确更新 DNS 记录,因此它指向的是网站而不是 VPS!

If anyone else runs into the same problem, check here to ensure the Remote Address is correct.如果其他人遇到同样的问题,请检查此处以确保Remote Address正确。 All I did was copy and paste it into my browser and when I ended up somewhere else, I knew something was wrong.我所做的只是将其复制并粘贴到我的浏览器中,当我最终到达其他地方时,我知道出了点问题。

远程地址

Thank you for your help, everyone, especially @dunhuang谢谢大家的帮助,特别是@dunhuang

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确设置ASP.NET开发服务器? - How do I set up my ASP.NET Development Server correctly? 如何将我的mysql会话设置为某个时区? - How do I set up my mysql session to be in a certain timezone? 我应该使用Amazon EC2或S3设置网站吗? 以及如何将其连接到数据库? - Should I set up my website with Amazon EC2 or S3? And how I connect it to a database? 如何为笔记本电脑上当前存在的Eclipse War文件和初始MySQL服务器数据设置在线托管? - How do I set up online hosting for an eclipse war file and initial MySQL server data currently on my laptop? 如何将服务器上的mysql数据库备份到计算机上? - how do i back up a mysql database on my server onto my computer? 我应该如何设置我的课程? - How should i set up my classes? 当我在数据库中输入connect()时,我的本地主机服务器显示为空白 - When I type in connect() to my database, my localhost server comes up blank 如何设置可以从同一网络中其他计算机访问的Mysql服务器? - How can I set up my Mysql server that can be accessed from other machines in the same network? 如何使用Objective-c连接到我的php / MySQL服务器? - How do I use Objective-c to connect to my php/MySQL server? 如何将Android应用程序连接到存储在服务器中的MySQL数据库? - How do I connect my Android Application to a MySQL database stored in a server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM