记录一次折腾了一下午的nginx配置文件问题

想着写一个后端使用gin + gorm + mysql,前端使用vue3 vite + element plus 的项目模板,但是在写docker-compose调试的时候,总是报404错误,后来发现是nginx配置文件的问题。

问题描述

在调试开发的时候 vite.config.ts 文件配置如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

export default (({ mode }) => {

  return defineConfig({
    plugins: [vue()],
    server: {
      port: 6060,
      proxy: {
        '/prod-api': {
          target: "http://127.0.0.1:4000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/prod-api/, '')
        },
      }
    },

  })
})

请求都能正常转发到后端。但是使用docker-compose时,却总是报404错误。/ect/nginx/nginx.conf内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

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

    server{
		  listen 80;
		  server_name location;
          location / {
              root   /usr/share/nginx/html;
			  try_files $uri $uri/ /index.html;
              index  index.html index.htm;
          }
          location /prod-api/ {
                proxy_set_header Host $http_host;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                rewrite ^/prod-api/(.*)$ /$1 break;
                proxy_pass http://177.7.0.12:4000/;
		  }

    }
}

发现问题

对比了ruoyi和gva的文档,都没什么问题,后来把80端口换成8080后就正常了,后来发现配置文件中有这么一行

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

然后看了下/etc/nginx/conf.d目录内有一个default.conf文件,内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

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

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

这里把80端口给抢了,正好目录文件位置都在/usr/share/nginx/html,所以最开始没有考虑到nginx配置文件的问题,一直以为是前端写的有问题。

解决问题

解决方案就是把/ect/nginx/nginx.conf里代码给注释掉就行。

1
2
3
4
5
6
...
#include /etc/nginx/conf.d/*.conf;
...
当然也可以不用80端口,或者删除`/etc/nginx/conf.d/default.conf`文件

或者在配置Docker镜像的时候把`default.conf`删除或者覆盖掉(推荐)