有关Nginx docker容器版部署访问403的解决

2023-12-14 00:59:17

问题记录

  • nginx.conf日志中出现,403问题,由于容器版的nginx中没有任何编辑工具,所以不太容器在容器中查看nginx文件的配置内容
  • 可能是权限问题,分析为主要是代理请求出现问题,没有代理到正确的远程服务器。从日志中可以看到,代理后的请求并没有发送到远程主机。
1.194.128.78 - - [04/Dec/2023:11:12:39 +0000] "GET /api/product/getBaseCategoryList HTTP/1.1" 403 
238 "http://47.92.131.235/home" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Mobile Safari/537.36 Edg/119.0.0.0"

解决结果

  • 经过小编的疯狂探索和尝试,从本地nginx的初始配置文件经过修改,最终,可以正常的配置文件内容如下:
  • 请根据自己的项目,基于本配置文件进行修改,进行尝试
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;


include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

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

    server {
        listen       80;
        server_name  _;
        # include /etc/nginx/default.d/*.conf;

	    location / {
		    root   #本地vue项目地址;
            try_files $uri $uri/ /index.html;
            index  index.html;
    	}
	    location /api {
            proxy_pass 远程主机地址; #http://xxx.xxx.xxx.xxx
	    }
        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }


server {
        listen       8080;
        server_name  localhost;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

        location / {
            root   #本地vue项目地址;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
		# 监听的请求location 请根据时机项目内容进行配置
        location /dev-api/ {
            proxy_pass 远程主机地址; #http://xxx.xxx.xxx.xxx
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
            rewrite ^/dev-api/(.*)$ /$1 break;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


文章来源:https://blog.csdn.net/yang2330648064/article/details/134819814
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。