nginx配置跨域请求,后台不用配置啦,完美

发布于:2025-05-24 ⋅ 阅读:(18) ⋅ 点赞:(0)

允许全部把域名改*

server {  
    listen       22222;  
    server_name  localhost;  
    location  / {  
        if ($request_method = 'OPTIONS') {  
            add_header Access-Control-Allow-Origin 'http://localhost:8080';  
            add_header Access-Control-Allow-Headers '*';  
            add_header Access-Control-Allow-Methods '*';  
            add_header Access-Control-Allow-Credentials 'true';  
            return 204;  
        }  
        if ($request_method != 'OPTIONS') {  
            add_header Access-Control-Allow-Origin 'http://localhost:8080' always;  
            add_header Access-Control-Allow-Credentials 'true';  
        }  
        proxy_pass  http://localhost:59200;   
    }  
}  

或者

server {  
    listen       22222;  
    server_name  localhost;  
    location  / {  
        add_header Access-Control-Allow-Origin 'http://localhost:8080' always;  
        add_header Access-Control-Allow-Headers '*';  
        add_header Access-Control-Allow-Methods '*';  
        add_header Access-Control-Allow-Credentials 'true';  
        if ($request_method = 'OPTIONS') {  
            return 204;  
        }  
        proxy_pass  http://localhost:59200;   
    }  
}  

完整配置,有lua


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        

        location / {
            root   html;
            index  index.html index.htm;
        }
      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
  
        
        
        # 处理以 /api 开头的请求
        location ^~ /main {
            if ($request_method = 'OPTIONS') {  
                add_header Access-Control-Allow-Origin 'http://localhost:5173';  
                add_header Access-Control-Allow-Headers '*';  
                add_header Access-Control-Allow-Methods '*';  
                add_header Access-Control-Allow-Credentials 'true';  
                return 204;  
            }  
            if ($request_method != 'OPTIONS') {  
                add_header Access-Control-Allow-Origin 'http://localhost:5173' always;  
                add_header Access-Control-Allow-Credentials 'true';  
            }
            access_by_lua_file "/usr/local/openresty/nginx/conf/lua/app.lua";
            # 将 /main 替换为空,只保留后续的路径
            rewrite ^/main(.*)$ $1 break;
            proxy_pass http://127.0.0.1:9001;
            # proxy_pass http://127.0.0.1:42002;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        # 处理 /wbrjPys/static 的请求
        location ^~ /wbrjPys/static {
            rewrite ^/wbrjPys(.*)$ $1 break;
            proxy_pass http://127.0.0.1:9090;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ^~ /wbrjPys {
            if ($request_method = 'OPTIONS') {  
                add_header Access-Control-Allow-Origin 'http://localhost:5173';  
                add_header Access-Control-Allow-Headers '*';  
                add_header Access-Control-Allow-Methods '*';  
                add_header Access-Control-Allow-Credentials 'true';  
                return 204;  
            }  
            if ($request_method != 'OPTIONS') {  
                add_header Access-Control-Allow-Origin 'http://localhost:5173' always;  
                add_header Access-Control-Allow-Credentials 'true';  
            } 
            access_by_lua_file "/usr/local/openresty/nginx/conf/lua/app.lua";
            # 将 /api 替换为空,只保留后续的路径
            rewrite ^/wbrjPys(.*)$ $1 break;
            proxy_pass http://127.0.0.1:9090;
            # proxy_pass http://127.0.0.1:42002;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ^~ /myGoApp {
            if ($request_method = 'OPTIONS') {  
                add_header Access-Control-Allow-Origin 'http://localhost:5173';  
                add_header Access-Control-Allow-Headers '*';  
                add_header Access-Control-Allow-Methods '*';  
                add_header Access-Control-Allow-Credentials 'true';  
                return 204;  
            }  
            if ($request_method != 'OPTIONS') {  
                add_header Access-Control-Allow-Origin 'http://localhost:5173' always;  
                add_header Access-Control-Allow-Credentials 'true';  
            }
            access_by_lua_file "/usr/local/openresty/nginx/conf/lua/app.lua";
            # 将 /myGoApp 替换为空,只保留后续的路径
            rewrite ^/myGoApp(.*)$ $1 break;
            proxy_pass http://127.0.0.1:8000;
            # proxy_pass http://127.0.0.1:42002;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


网站公告

今日签到

点亮在社区的每一天
去签到