(wss://)
내 로컬 서버에서 WebSocket 을 사용 하고 다음과 같이 잘 작동합니다.
그러나이 프로그램을 nginx를 역방향 프록시 및 유효한 SSL (certbor, 암호화하자)으로 구성 한 서버에 배포하면 내 프로그램이 작동하지 않고 Java 스크립트 clent 에서이 오류가 발생하고 거기에 몇 가지 세부 사항이 작성됩니다.
오류 브라우저 콘솔 :
콘텐츠 보안 정책 : 페이지의 설정이 ws : /// ( "default-src")에서 리소스로드를 차단했습니다.
오류가 가리키는 Java 스크립트 코드 :
var ws = new WebSocket('wss://' + location.host + '/<some-url>');
Nginx 구성 :
user nginx;
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/conf-enabled/*.conf;
include /etc/nginx/sites-enabled/*.conf;
server {
listen 443;
listen [::]:443 ;
server_name www.<my-domain> <my-domain>;
location / {
proxy_pass http://127.0.0.1:<my-port>/;
proxy_redirect off;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass_request_headers on;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
}
ssl_certificate /etc/letsencrypt/live/<my-domain>/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/<my-domain>/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
}
두 가지 점이 있습니다.
add_header Content-Security-Policy "default-src 'self'";
. 구성에 다음과 같은 내용이 표시되지 않습니다 . @EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) {
http
.headers(headers -> headers
.contentSecurityPolicy(csp -> csp
.policyDirectives("default-src 'self'")
)
);
}
}
또는:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers()
.contentSecurityPolicy("default-src 'self'");
}
}
있는지 확인 'self'
하거나 ws:
기존 규칙에 추가 하십시오 . https : 페이지 'self'
는 교활합니다. https : 및 wss : 만 허용합니다 (http : 및 ws : 아님-어쨌든 혼합 콘텐츠로 차단됨). http : 페이지에서 'self'는 https : / wss : / http : / ws :를 허용합니다. CSP에
특정 connect-src 'self'
또는 connect-src ws://site.com wss:site.com
지시문을 추가하는 것이 더 좋을 수 있습니다 .
CSP가 필요하지 않은 경우 관련 항목을 제거하십시오.
http.headers()
.contentSecurityPolicy("default-src 'self'");
blocked the loading of a resource at ws:///
: ///는 in new WebSocket('wss://' + location.host + '/<some-url>')
location.host
이 비어 있음을 의미 하므로 websocket에 연결할 위치가 없습니다. 모든 CSP는 ws:///
소스를 유효하지 않은 것으로 차단 합니다.이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다