Skip to content

Rule Configuration file

Yòmá edited this page Jan 17, 2023 · 32 revisions

Nginx location directive

Rule


// It is not a requirement that the line ends in a semicolon(;). - optional
server {
    listen                        [ IP: ]PORT [ default_server ];
    *1[ root                      PATH; ]
    *1[ server_name               NAME; ]
    * [ error_page                [ (*DIGIT)(SP) ]   (PATH)FILE_NAME.html; ]
    
    location /URI {
        accepted_methods          ( GET | HEAD | POST | PUT | DELETE | ALL );
        *1[ redirect              URI; ]
        *1[ root                  PATH; ]
        *1[ index                 (PATH)FILE_NAME.html; ] 
        *1[ autoindex             ( on | off ); ]
        *1[ saved_path            PATH;
        *1[ cgi_pass              CGI_FILE_NAME; ]
        *1[ max_body_size  DIGIT[ K | M ]; ]
    }

    location .CGI_EXTENSION {
        accepted_methods          ( GET | HEAD | POST | PUT | DELETE | ALL );
        *1[ cgi_path_info         FULL_PATH_CGI_EXEC_PROGRAM; ]
        *1[ cgi_pass              CGI_FILE_NAME; ]
        *1[ saved_path            PATH; ]
    }

}

Example

server {
    listen               127.0.0.1:8080 default_server;
    server_name          spaceX;
    root                 /usr/goinfre/spacex;
    error_page           /www/error.html;
    error_page           404 403 /www/error40X.html;
    error_page           504 503 507 520 /www/error50X.html;

    
    location / {
        accepted_methods GET HEAD;
        root             /www/;
        index            index.html;
        autoindex        on;
        max_body_size 200M;
    }

    location /upload {
        accepted_methods POST PUT;
        autoindex        on
        saved_path       /www/upload/; 
    }

    location /github {
        accepted_methods GET HEAD;
        redirect         https://github.com; 
    }

}

server {
    listen               127.0.0.1:8080;
    server_name          spaceX_another_server;
    error_page           /www/error.html;
    
    location .py {
        accepted_methods PUT POST;
        cgi_path_info    /usr/bin/python3;
    }
}




Subject Requirement

  • Choose the port and host of each ’server’

listen [ IP: ]PORT;


  • Setup the server_names or not.

server_name NAME;


  • The first server for a host:port will be the default for this host:port

listen [ IP: ]PORT [ default_server ];


  • Setup default error pages.

error_page /custom_40X.html; <- any other case

*[ error_page [1*DIGIT] PATH/FILE_NAME.html; ] <- specific case


  • Limit client body size.

max_body_size DIGIT( K | M );



  • Setup routes with one or multiple of the following rules/configuration
location URI_RULE {
}

  • Define a list of accepted HTTP methods for the route.

accepted_methods ( GET | POST | PUT | DELETE | ... );


  • Define a HTTP redirection

redirect URI;


  • Define a directory or a file from where the file should be searched

root DIR;


  • Turn on or off directory listing.

autoindex ( on | off );


  • Set a default file to answer if the request is a directory.

index FILE;



  • Execute CGI based on certain file extension (for example .php). Make the route able to accept uploaded files and configure where they should be saved.
  location /upload {
	cgi_pass      CGI_FILE_NAME;
	saved_path    SHOULD_BE_SAVED;
	cgi_path_info CGI_LOCATED_PATH;
  }

  • Because you won’t call the CGI directly, use the full path as PATH_INFO.

cgi_path_info FULL_PATH_CGI(can be relative path);


  • Just remember that, for chunked request, your server needs to unchunked it and the CGI will expect EOF as end of the body.

chunked로 받고, cgi에게 줄때 EOF 마무리 하기


  • Same things for the output of the CGI. If no content_length is returned from the CGI, EOF will mark the end of the returned data.

Vice Versa


  • Your program should call the CGI with the file requested as first argument.

??


  • The CGI should be run in the correct directory for relative path file access.

??


Clone this wiki locally