nginx配置支持ws,并解决跨域

修改配置文件nginx.conf

 server {
     listen 443 ssl; 
     server_name excalidraw.haimaotv.com; 
     ssl_certificate /opt/nginx-1.24.0/excalidraw.haimaotv.com_bundle.crt; 
     ssl_certificate_key /opt/nginx-1.24.0/excalidraw.haimaotv.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 / {
      //解决跨域
       if ($request_method = 'OPTIONS') {
                add_header Access-Control-Allow-Origin 'https://ottepg-moss.cedock.com';
                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 'https://ottepg-moss.cedock.com' always;
			add_header Access-Control-Allow-Credentials 'true';
		}

        proxy_pass http://127.0.0.1:3002; # 将请求代理转发到本地3002端口的服务
        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;
        //支持websocket
	    proxy_http_version 1.1;
	    proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_cache_bypass $http_upgrade;
      }
    }

  

posted @ 2024-01-23 17:16  Johnson_wang  阅读(521)  评论(0编辑  收藏  举报