Install
Follow the official nginx tutorial to install nginx on Ubuntu
/en/linux_packages.html#Ubuntu
According to the above installation, we will register the service of nginx for us, and we can use itservice nginx start
To start nginx
Configuration File
The default configuration file is:, the storage location is
/usr/local/nginx/conf
, /etc/nginx
, or /usr/local/etc/nginx
.
Commonly used commands
After nginx is started, we can use-s
Parameters to execute some control commands
3.1 Help Command
nginx -? nginx -h
3.2 Quick stop
nginx -s stop
3.3 Elegant Stop
nginx -s quit
3.4 Reload the configuration file
nginx -s reload
Once the main process receives a signal to reload the configuration, it will check the syntax validity of the new configuration file and try to apply the configuration in it.
If the new configuration is applied successfully, the main process will start the new worker process and send a message to the old project requesting them to be closed.
If the new configuration fails, the main process rolls back the configuration changes and continues to use the old configuration.
3.5 Reopen the log file
nginx -s reopen
3.6 nginx main process id
The id of nginx main process will be written in/usr/local/nginx/logs
or/var/run
In the directoryIn the file
We can also kill the nginx main process by following command:
# pid means main process idkill -s quit pid
3.7 View nginx process
ps -ax | grep nginx
3.8 Run as specified configuration file
nginx -c file
4. nginx static proxy
server { listen 80; # Listen to the port server_name localhost; # Service name, corresponding IP or domain name #charset koi8-r; #access_log logs/ main; location / { # If you access localhost, the request will be forwarded to the html folder in the root directory of nginx or root html; index ; } #error_page 404 /; # redirect server error pages to the static page / error_page 500 502 503 504 /; location = / { # Server error will be forwarded to the html folder in the root directory of nginx root html; } }
5 nginx as a proxy server
Willlocalhost:8082/api/test
Request forward tohttp://192.168.1.92:8080/test
proxy_pass http://192.168.1.92:8080/;
The ending/
Must be there, otherwise the request will be forwarded tohttp://192.168.1.92:8080/api/test
http { sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; server { listen 8082; server_name localhost; location /api/ { proxy_pass http://192.168.1.92:8080/;# This ending must have /, otherwise the request will be proxied to http://192.168.1.92:8080/api/test } } }
6. Load balancing
Policies for load balancing include polling, minimum number of connections, iphash, and weight. The load balancing policy used by default is polling.
6.1 Poll
http { upstream myapp1 { server ; server ; server ; } server { listen 80; location / { proxy_pass http://myapp1; } } }
6.2 Minimum connection
http { upstream myapp1 { least_conn; server ; server ; server ; } server { listen 80; location / { proxy_pass http://myapp1; } } }
6.3 ip hash
http { upstream myapp1 { ip_hash; server ; server ; server ; } server { listen 80; location / { proxy_pass http://myapp1; } } }
6.4 Weight
http { upstream myapp1 { server weight=3; server ; server ; } server { listen 80; location / { proxy_pass http://myapp1; } } }
6.5 Related Parameters
# Set the server's weight value, the default is 1weight=number # Maximum number of connections, limiting the maximum number of connections to the proxy server, the default value is 0, indicating that there is no limitmax_conns=number # Unavailable time of the server, when the connection failsfail_timeout=time # Mark the server as a standby. When the main server is unavailable, request the standby serverbackup # Mark the server as stopdown
acting
http { upstream backend { server ; } server { listen 80; location /chat/ { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.