How to fix nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf

Posted October 30, 2019 17.1k views

Please help me
How to fix

nginx: [emerg] “server” directive is not allowed here in /etc/nginx/nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer


You would need to open the /etc/nginx/nginx.conf file with your favorite text editor, go to line 5 and comment out the server directive. Then run the test restart again.

If you are not sure, feel free to share your nginx.conf file here and I could advise you further.


  • worker_processes  auto;
    events {
        worker_connections  1024;
    # RTMP configuration
    rtmp {
        server {
            listen 1935; # Listen on standard RTMP port
            chunk_size 4000;
            application show {
                live on;
                # Turn on HLS
                hls on;
                hls_path /nginx/hls/;
                hls_fragment 3;
                hls_playlist_length 60;
                # disable consuming the stream from nginx as rtmp
                deny play all;
    http {
        sendfile off;
        tcp_nopush on;
        # aio on;
        directio 512;
        default_type application/octet-stream;
        server {
            listen 8080;
            location / {
                # Disable cache
                add_header 'Cache-Control' 'no-cache';
                # CORS setup
                add_header 'Access-Control-Allow-Origin' '*' always;
                add_header 'Access-Control-Expose-Headers' 'Content-Length';
                # allow CORS preflight requests
                if ($request_method = 'OPTIONS') {
                    add_header 'Access-Control-Allow-Origin' '*';
                    add_header 'Access-Control-Max-Age' 1728000;
                    add_header 'Content-Type' 'text/plain charset=UTF-8';
                    add_header 'Content-Length' 0;
                    return 204;
                types {
                    application/dash+xml mpd;
                    application/ m3u8;
                    video/mp2t ts;
                root /nginx/;
    edited by bobbyiliev