nginx has one master process and several worker processes.The main purpose of the master process is to read and evaluate configuration,and maintain worker processes.Worker processes do actual processing of requests.nginx employs event-based model and OS-dependent mechanisms to efficientlydistribute requests among worker processes.The number of worker processes is defined in the configuration file andmay be fixed for a given configuration or automatically adjusted to thenumber of available CPU cores (seeworker_processes).
The way nginx and its modules work is determined in the configuration file.By default, the configuration file is named nginx.confand placed in the directory/usr/local/nginx/conf,/etc/nginx, or/usr/local/etc/nginx.
Once the master process receives the signal to reload configuration,it checks the syntax validityof the new configuration file and tries to apply the configuration providedin it.If this is a success, the master process starts new worker processesand sends messages to old worker processes, requesting them toshut down.Otherwise, the master process rolls back the changes andcontinues to work with the old configuration.Old worker processes, receiving a command to shut down,stop accepting new connections and continue to service current requests untilall such requests are serviced.After that, the old worker processes exit.
nginx consists of modules which are controlled by directives specifiedin the configuration file.Directives are divided into simple directives and block directives.A simple directive consists of the name and parameters separated by spacesand ends with a semicolon (;).A block directive has the same structure as a simple directive, butinstead of the semicolon it ends with a set of additional instructionssurrounded by braces ( and ).If a block directive can have other directives inside braces,it is called a context (examples:events,http,server,andlocation).
Directives placed in the configuration file outsideof any contexts are considered to be in themain context.The events and http directivesreside in the main context, serverin http, and location inserver.
An important web server task is serving outfiles (such as images or static HTML pages).You will implement an example where, depending on the request,files will be served from different local directories: /data/www(which may contain HTML files) and /data/images(containing images).This will require editing of the configuration file and setting up of aserverblock inside the httpblock with two locationblocks.
Next, open the configuration file.The default configuration file already includes several examples ofthe server block, mostly commented out.For now comment out all such blocks and start a newserver block:
This is already a working configuration of a server that listenson the standard port 80 and is accessible on the local machine at response to requests with URIs starting with /images/,the server will send files from the /data/images directory.For example, in response to the request nginx willsend the /data/images/example.png file.If such file does not exist, nginx will send a responseindicating the 404 error.Requests with URIs not starting with /images/ will bemapped onto the /data/www directory.For example, in response to the request nginx willsend the /data/www/some/example.html file.
One of the frequent uses of nginx is setting it up as a proxy server, whichmeans a server that receives requests, passes them to the proxied servers,retrieves responses from them, and sends them to the clients.
We will configure a basic proxy server, which serves requests ofimages with files from the local directory and sends all other requests to aproxied server.In this example, both servers will be defined on a single nginx instance.
This will be a simple server that listens on the port 8080(previously, the listen directive has not been specifiedsince the standard port 80 was used) and mapsall requests to the /data/up1 directory on the localfile system.Create this directory and put the index.html file into it.Note that the root directive is placed in theserver context.Such root directive is used when thelocation block selected for serving a request does notinclude its own root directive.
Next, use the server configuration from the previous sectionand modify it to make it a proxy server configuration.In the first location block, put theproxy_passdirective with the protocol, name and port of the proxied server specifiedin the parameter (in our case, it is :8080):
We will modify the second locationblock, which currently maps requests with the /images/prefix to the files under the /data/images directory,to make it match the requests of images with typical file extensions.The modified location block looks like this:
When nginx selects a location block to serve a requestit first checks locationdirectives that specify prefixes, remembering locationwith the longest prefix, and then checks regular expressions.If there is a match with a regular expression, nginx picks thislocation or, otherwise, it picks the one remembered earlier.
The most basic nginx configuration to work with a FastCGI serverincludes using thefastcgi_passdirective instead of the proxy_pass directive,and fastcgi_paramdirectives to set parameters passed to a FastCGI server.Suppose the FastCGI server is accessible on localhost:9000.Taking the proxy configuration from the previous section as a basis,replace the proxy_pass directive with thefastcgi_pass directive and change the parameter tolocalhost:9000.In PHP, the SCRIPT_FILENAME parameter is used fordetermining the script name, and the QUERY_STRINGparameter is used to pass request parameters.The resulting configuration would be:
In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file config/application.rb and environment-specific configuration files (such as config/environments/production.rb) allow you to specify the various settings that you want to pass down to all of the components.
If you need to apply configuration directly to a class, use a lazy load hook in an initializer to avoid autoloading the class before initialization has completed. This will break because autoloading during initialization cannot be safely repeated when the app reloads.
config.load_defaults loads default configuration values for a target version and all versions prior. For example, config.load_defaults 6.1 will load defaults for all versions up to and including version 6.1.
Says whether autoload paths have to be added to $LOAD_PATH. It is recommended to be set to false in :zeitwerk mode early, in config/application.rb. Zeitwerk uses absolute paths internally, and applications running in :zeitwerk mode do not need require_dependency, so models, controllers, jobs, etc. do not need to be in $LOAD_PATH. Setting this to false saves Ruby from checking these directories when resolving require calls with relative paths, and saves Bootsnap work and RAM, since it does not need to build an index for them.
Takes a block which will be run after Rails has finished initializing the application. That includes the initialization of the framework itself, engines, and all the application's initializers in config/initializers. Note that this block will be run for rake tasks. Useful for configuring values set up by other initializers:
Sets the host for the assets. Useful when CDNs are used for hosting assets, or when you want to work around the concurrency constraints built-in in browsers using different domain aliases. Shorter version of config.action_controller.asset_host.
Makes application believe that all requests are arriving over SSL. This is useful when proxying through a load balancer that terminates SSL, the forwarded request will appear as though it's HTTP instead of HTTPS to the application. This makes redirects and cookie security target HTTP instead of HTTPS. This middleware makes the server assume that the proxy already terminated SSL, and that the request really is HTTPS.
Accepts an array of paths from which Rails will autoload constants that won't be wiped per request. Relevant if reloading is enabled, which it is by default in the development environment. Otherwise, all autoloading happens only once. All elements of this array must also be in autoload_paths. Default is an empty array.
Configures which cache store to use for Rails caching. Options include one of the symbols :memory_store, :file_store, :mem_cache_store, :null_store, :redis_cache_store, or an object that implements the cache API. Defaults to :file_store. See Cache Stores for per-store configuration options.
Is a flag. If true then any error will cause detailed debugging information to be dumped in the HTTP response, and the Rails::Info controller will show the application runtime context in /rails/info/properties. true by default in the development and test environments, and false in production. For finer-grained control, set this to false and implement show_detailed_exceptions? in controllers to specify which requests should provide debugging information on errors.
Controls whether or not someone can start a console in sandbox mode. This is helpful to avoid a long running session of sandbox console, that could lead a database server to run out of memory. Defaults to false.
If config.enable_reloading is true, application classes and modules are reloaded in between web requests if they change. Defaults to true in the development environment, and false in the production environment.
Exceptions applications need to handle ActionDispatch::Http::MimeNegotiation::InvalidType errors, which are raised when a client sends an invalid Accept or Content-Type header.The default ActionDispatch::PublicExceptions application does this automatically, setting Content-Type to text/html and returning a 406 Not Acceptable status.Failure to handle this error will result in a 500 Internal Server Error.
Is the class used to detect file updates in the file system when config.reload_classes_only_on_change is true. Rails ships with ActiveSupport::FileUpdateChecker, the default, and ActiveSupport::EventedFileUpdateChecker (this one depends on the listen gem). Custom classes must conform to the ActiveSupport::FileUpdateChecker API.
c80f0f1006