Detailed Introduction to Nginx Installation and Configuration Rules

Detailed Introduction to Nginx Installation and Configuration Rules

1. Installation and operation of nginx (Mac OS environment)

1. Install nginx

Can be installed directly via Homebrew:

$brew install nginx

After installation, the default homepage file is in the /usr/local/var/www folder

The default configuration file address is /usr/local/etc/nginx/nginx.conf

Nginx uses port 8080 by default. If you find that the port is occupied (check the port occupancy through $lsof -i:8080 ), you can kill the process using the port ( $kill 進程PID ). Or modify the default port of nginx ( /usr/local/etc/nginx/nginx.conf )

2. Start nginx

$brew services start nginx

Or go to the directory /usr/local/bin and type $./nginx

After successful startup, visit http://localhost:8080/ in your browser, and you can see the static resources returned by the nginx server (the default is /usr/local/var/www/index.html)

3. Stop nginx

$ nginx -s stop

4. Restart nginx

$ nginx -s reload

5. View nginx configuration path information

$brew info nginx

2. nginx rule configuration

More configurations can be viewed

https://www.nginx.com/resources/wiki/start/#pre-canned-configurations

http://nginx.org/en/docs/

http://www.nginx.cn/doc/

1. location

location grammar articles

2. root and alias

In nginx, you can specify the access path of resources through root and alias.

1) root:

location / {
  root /usr/local/var/www/;
  index index.html index.htm;
}

In the above rule, when the address http://localhost:8080/index.html is requested, the resource accessed is: /usr/local/var/www/index.html.

When requesting the address http://localhost:8080/test/a.png , the resource accessed is: /usr/local/var/www/test/a.png.

In other words, the resource address accessed is actually the path specified by root + the path matched by location.

2) alias:

alias is an alias, and its matching rules are slightly different from those of root.

location /a/ {
  alias /usr/local/var/www/b/;
}

In the above rule, when requesting the address http://localhost:8080/a/ , the resource accessed is: /usr/local/var/www/b/index.html.

When requesting the address http://localhost:8080/a/1.gif , the resource accessed is: /usr/local/var/www/b/1.gif.

In other words, the resource address accessed is the path specified by alias, and has nothing to do with the path matched by location (the path matched by location will be discarded).

3) The difference between root and alias:

Alias ​​can only be used in location, while root can exist in server, http and location.

The alias must be followed by a “/”, otherwise the file will not be found, while the “/” is optional for root.

3. try_file

location /test/ {
  try_files $uri $uri/ /a/1.png;
}

try_files tries to read the files accessed by the user from the website directory. If the first variable exists, it returns directly; if it does not exist, it continues to read the second variable. If it exists, it returns directly; if it does not exist, it jumps to the third parameter.

$uri is an nginx variable that stores the address accessed by the user. For example, when visiting http://www.xxx.com/index.html, \$uri is /index.html.

$uri/ represents a directory being accessed, for example: http://www.xxx.com/hello/test/, then \$uri/ is /hello/test/.

For example, in the above rule: when requesting the address http://localhost:8080/test/2.png , try_files will determine whether it is a file or a directory. It turns out that it is a file, which matches the first parameter $uri variable. Then go to the website directory to find out whether the test/2.png file exists. If it exists, read it and return it directly. If it does not exist, jump to the third parameter, which returns the website root directory + /a/1.png file (/usr/local/var/www/a/1.png).

More usage: https://www.jb51.net/article/156899.htm

4. rewrite

rewrite syntax

The rewrite function is to implement URL rewriting and redirection.

Syntaxrewrite rewrite regex replacement [flag];

rewrite can only be placed in server{} , location{} , if{} , and can only work on the string after the domain name excluding the passed parameters. For example, http://www.xxx.com/a/b/index.html?param=1&u=str will only rewrite /a/b/index.html.

The execution order of rewrite is:

  • Execute the rewrite directive of the server block
  • Perform location matching
  • Execute the rewrite directive in the selected location

flag flag:

  • last : Equivalent to Apache's [L] flag, indicating that rewrite is complete
  • break: Stop executing the subsequent rewrite instruction set of the current virtual host
  • redirect : Return 302 temporary redirect, the address bar will show the redirected address
  • permanent: Returns a 301 permanent redirect, and the address bar will display the redirected address
location /home/ {
  rewrite ^/home/test/ http://www.baidu.com;
}

The above rule: when you access the address http://localhost:8080/home/test/ , the page will be redirected to http://www.baidu.com.

Some tips:

How to redirect url in nginx without changing the url display in the browser?

proxy_pass can specify a reverse proxy

More usage: https://www.jb51.net/article/134233.htm

3. Some command line configuration (mac OS)

1. How to open a file with vscode in the command line

cd /usr/local/bin/
ln -s "/Applications/Visual Studio Code.app/Contents/MacOS/Electron" vscode

Among them, /Applications/Visual Studio Code.app/Contents/MacOS/Electron is the executable file of vscode, and the ln -s command is to put it in the /usr/local/bin/ directory through a soft link. This way you can open the file through the vscode command elsewhere in the command line.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • CentOS 6.4 installation and configuration of LNMP server (Nginx+PHP+MySQL)
  • Tutorial on installing and configuring nginx to support PHP (full)
  • Install and configure php-fpm to build a production environment of Nginx+PHP
  • Detailed explanation of Nginx installation environment configuration under Mac
  • Tutorial on installing and configuring PHPMyAdmin on Nginx server
  • Summary of Nginx server installation and some basic configuration
  • Nginx Simple Installation and Configuration Method Graphic Tutorial

<<:  How to avoid the trap of URL time zone in MySQL

>>:  React gets input value and submits 2 methods examples

Recommend

Let IE support CSS3 Media Query to achieve responsive web design

Today's screen resolutions range from as smal...

How to develop Java 8 Spring Boot applications in Docker

In this article, I will show you how to develop a...

HTML page common style (recommended)

As shown below: XML/HTML CodeCopy content to clip...

Example code for CSS columns to achieve two-end alignment layout

1. Going around in circles After going around in ...

A method of making carousel images with CSS3

Slideshows are often seen on web pages. They have...

Detailed explanation of error handling examples in MySQL stored procedures

This article uses an example to describe the erro...

Practical example of nested routes in vue.js Router

Table of contents Preface Setting up with Vue CLI...

How to set static IP for Ubuntu 18.04 Server

1. Background Netplan is a new command-line netwo...

Solution to Django's inability to access static resources with uwsgi+nginx proxy

When deploying uwsgi+nginx proxy Django, access u...

Docker uses nextcloud to build a private Baidu cloud disk

Suddenly, I needed to build a private service for...

How to use Vue3 mixin

Table of contents 1. How to use mixin? 2. Notes o...

Use Docker to build a Git image using the clone repository

Overview I have been using Docker for more than a...

How to improve MySQL Limit query performance

In MySQL database operations, we always hope to a...