2

各位前辈, 能否帮忙看看我错误的 nginx 配置的问题是什么, 虽然我最后问 ChatGPT 得...

 1 year ago
source link: https://www.v2ex.com/t/925099
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  问与答

各位前辈, 能否帮忙看看我错误的 nginx 配置的问题是什么, 虽然我最后问 ChatGPT 得到正确的配置,但是依然不知道我自己错在哪里

  zzping · 52 分钟前 · 147 次点击
nginx 配置了两天了,还是配置不出,我是真的菜。
其中放弃去补了一些 nginx 的知识依然配不出,最后问的 GhatGPT 出来的。
主要问题:http 正常,https 就无法通行。(原因很多,证书的原因,代理的原因,最主要的原因应该是我配置冲突了)
---------废话补多说---上代码-----------------------
--------------------------------------------------
我:
我的域名是 www.v2ex.com,它正常解析到 120.00.00.93 ,我的 80 ,8001 端口正常开放
我有个 Django 程序,python3 manage.py runserveri 8001
我的前端文件是 root /home/www/dist;
现在需要你配置下 nginx
1 、要求能通过 v2ex.com 访问我的网站
2 、要求配置 https,其中证书文件域名都是 v2ex.com 的
    ssl_certificate /home/Nginx/1_v2ex.com_bundle.crt; 
    #请填写私钥文件的相对路径或绝对路径
    ssl_certificate_key /home/Nginx/2_v2ex.com.key; 
3 、对其中的 api 开头的都转到我的 django 程序 8001 端口
4 、请注意 https 的问题

------ChatGPT-------------------------------------
server {
    listen 80;
    listen [::]:80;
    server_name v2ex.com www.v2ex.com;
    return 301 https://$server_name$request_uri;
}
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name v2ex.com www.v2ex.com;
    # SSL 证书配置
    ssl_certificate /home/Nginx/1_v2ex.com_bundle.crt;
    ssl_certificate_key /home/Nginx/2_v2ex.com.key;
    location / {
        root /home/www/dist;
        try_files $uri $uri/ /index.html;
    }
    location /api {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
------ChatGPT-------------------------------------
------我的错误配置-------------------------------------
    server {
        listen 80;
        server_name 120.00.000.93; #打码处理

        # 配置静态文件目录
        location / {
            root /home/www/dist;
            try_files $uri $uri/ /index.html;
        }
        
        location /api {
            proxy_pass http://120.00.000.93:8001;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
server {
    listen 443 ssl; 
    server_name www.v2ex.com;
    ssl_certificate /home/Nginx/1_v2ex.com_bundle.crt; 
    ssl_certificate_key /home/Nginx/2_v2ex.com.key; 
    ssl_session_timeout 5m;
    ssl_protocols TLSv1.2 TLSv1.3; 
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
    ssl_prefer_server_ciphers on;

  location / {
        root /home/www/dist;
        # 用于配合 browserHistory 使用
        try_files $uri $uri/ /index.html;

  }
  location /api {
      proxy_pass https://localhost:8001/;
      proxy_set_header   X-Forwarded-Proto $scheme;
      proxy_set_header   Host              $http_host;
      proxy_set_header   X-Real-IP         $remote_addr;
  }
}
------我的错误配置-------------------------------------

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK