Using nginx-rtmp causes cross-domain problems

eando2012 注册会员
2023-02-28 11:37

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > this configuration file and there is no obvious cross-domain configuration, if your application needs to cross domain access, You can add the CORS(cross-domain Resource Sharing) configuration to the Nginx configuration file. Here is a simple example that you can add to the Nginx configuration file:

http {
    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';

This configuration will allow all domain names to access your application and allow GET, POST, and OPTIONS requests. In addition, it will also allow some common HTTP headers.

Please note that this configuration is a simple example and you may need to adjust it to your actual needs.

wen119260688 注册会员
2023-02-28 11:37

The error message is caused by the browser blocking the cross-domain request. In browsers, cross-domain requests from JavaScript scripts are usually restricted by the same origin policy, which means that the resources of one domain cannot be accessed directly by JavaScript from a page of another domain.

To solve this problem, you need to add some cers-related Settings to the nginx configuration file to allow cross-domain requests to access your resources. To do this, add the following content to the configuration of the HTTP Server:

location /hls {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header Access-Control-Expose-Headers 'Content-Length,Content-Range';
    types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
    alias temp/hls;
    expires -1;

The add_header directive here is used to add an HTTP response header, which adds information about CORS to the HTTP response and tells the browser which cross-domain requests are allowed to access the resource.

Note that opening all sources with wildcard *(i.e., Access-Control-Allow-Origin *) may have security risks. It is recommended that you specify specific sources as required.

Also, there seem to be some typos and grammatical problems in your code, such as his fraoment 8s: and expires -l; . It is recommended to check these places for correct spelling and grammar.

About the Author

Question Info

Publish Time
2023-02-28 11:37
Update Time
2023-02-28 11:37