nginx解决域名代理到IP+端口的平台静态资源和接口地址问题

主要解决静态资源访问404问题,接口地址重新代理问题。

nginx 复制代码
server {
        listen 80;
        server_name youdomain;
        charset utf8;
        
        listen 443 ssl;
        ssl_certificate /usr/local/nginx/conf/cert/youdomain.pem;
        ssl_certificate_key /usr/local/nginx/conf/cert/youdomain.key;
        ssl_session_timeout 3m;##超时时间
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
     

        #root    /home/front/console-front/;
        #root    /home/cloud/;#https验证文件目录
        index  index.php index.html;

        location / {
                try_files $uri $uri/ /index.php;
		if (!-e $request_filename) {
          		rewrite ^/(.*) /index.html last;
            		break;
        	}
           	proxy_pass http://ip:port/;
        	proxy_set_header Host $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;
        }

	#在线设备检测系统
        #location ^~ /efl-prod/ {
        #   	proxy_pass http://ip:port/;
        #	proxy_set_header Host $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;
        #}
	#解决js css 访问不到的问题
	#location ~ .* {
   	#	proxy_pass http://ip:port;
        #   	#alias /data/front/nx-travel-front/;
   	#	proxy_set_header Host $http_host;
   	#	proxy_set_header X-Real-IP $remote_addr;
   	#	proxy_set_header X-Forw $proxy_add_x_forwarded_for;
	#}
    	# 静态资源代理 - 必须添加!!!
    	location ^~ /assets/ {
        	proxy_pass http://ip:port/assets/;
        	proxy_set_header Host $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;
        
        	# 静态资源缓存
        	expires 30d;
        	add_header Cache-Control "public, immutable";
    	}
    
    	location ^~ /equipManage/ {
        	proxy_pass http://ip:port/sysManage/;
        	proxy_set_header Host $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;
        
        	# 静态资源缓存
        	expires 30d;
        	add_header Cache-Control "public, immutable";
    	}
    
    	# 可能还有其他静态资源路径
    	location /static/ {
        	proxy_pass http://ip:port/static/;
        	proxy_set_header Host $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;
        	expires 30d;
    	}
    
    	# CSS文件
   	location /css/ {
        	proxy_pass http://ip:port/css/;
        	proxy_set_header Host $host;
        	# ...其他头部配置
    	}
    
    	# JS文件
    	location /js/ {
        	proxy_pass http://ip:port/js/;
        	proxy_set_header Host $host;
        	# ...其他头部配置
    	}
    
    	# 图片
    	location /images/ {
        	proxy_pass http://ip:port/images/;
        	proxy_set_header Host $host;
        	# ...其他头部配置
    	}

    	# API接口特殊处理(如果需要)
    	location ~ ^/gateway/ {
        	proxy_pass http://ip:port;
        	proxy_set_header Host $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;
        
        	# API接口超时设置长一些
        	proxy_connect_timeout 60s;
        	proxy_send_timeout 60s;
        	proxy_read_timeout 120s;
        
        	# 禁用API缓存
        	add_header Cache-Control "no-cache, no-store, must-revalidate";
        	add_header Pragma "no-cache";
        	add_header Expires "0";
    	}

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
        location ~* \.(css|png|jpg|jpeg|ico)$ {
                expires max;
                log_not_found off;
        }
        location ~ /(assets|upfile|uploads|tmp|log|css|js|images|statics|demo)/.*\.(php|php5)?$ {
                #deny all;
				allow all;
        }

        location ~ /(\.svn|\.git|\.ht|\.DS) {
                deny all;
                internal;
        }
        location ~ \.php($|/) {
                 fastcgi_pass   127.0.0.1:9000;
                 fastcgi_index  index.php;
                 fastcgi_split_path_info  ^(.+\.php)(.*)$;
                 fastcgi_param  PATH_INFO $fastcgi_path_info;
                 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
            fastcgi_param  CI_ENV 'production';
            include        fastcgi_params;
                 #include        fastcgi.conf;
        }

        #if (!-e $request_filename) {
        #    rewrite ^/(.*)$ /index.php/$1 last;
        #    break;
        #}

        access_log  /data/nginx/logs/efl-prod.access.log;
        error_log  /data/nginx/logs/efl-prod.error.log;
    }

*********************************只要思想不滑坡,办法总比困难多*********************************

相关推荐
I'm a winner15 小时前
【IP核】 Xilinx FPGA LVDS 高速接口,含验证工程与板级测试用例
tcp/ip·fpga开发·测试用例
画中有画15 小时前
使用yolov8实现自动化脚本
运维·yolo·自动化
m0_7408596216 小时前
Nginx进行配置文件拆分(以windows解压版为例)
nginx
Piko61417 小时前
H3C IRF2 堆叠实战:打造高可靠核心交换网络
运维·网络·笔记
开发小程序的之朴18 小时前
认识安企CMS-安装安企CMS的环境要求
nginx·golang·系统架构
万联WANFLOW20 小时前
SD-WAN 控制平面高可用怎么做?SDWAN 控制器挂了,全网会发生什么
运维·网络·分布式·架构
SkyWalking中文站1 天前
认识 Horizon UI · AI Assistant:用日常语言查询你的可观测性数据
运维·监控·自动化运维
艾莉丝努力练剑1 天前
OpenCode AI 编程:Ubuntu 24.04 环境安装与使用指南
linux·服务器·网络·人工智能·tcp/ip·ubuntu
崇山峻岭之间1 天前
Keil5输出hex转换为bin的设置
linux·运维·服务器
Hoxy.R1 天前
KingbaseES读写分离高可用集群扩容、备库重建与故障切换实战
运维·数据库