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

Docker case analysis: Building a Redis service

Table of contents 1 Create mount directories and ...

Detailed steps for completely uninstalling MySQL 5.7

This article mainly summarizes various problems o...

Implementing custom scroll bar with native js

This article example shares the specific code of ...

JavaScript implements Tab bar switching effects

Here is a case that front-end developers must kno...

Example of usage of keep-alive component in Vue

Problem description (what is keep-alive) keep-ali...

Detailed process of using vmware to test PXE batch installation server

Table of contents 1. Preparation 1. Prepare the e...

iview implements dynamic form and custom verification time period overlap

Dynamically adding form items iview's dynamic...

Linux/Mac MySQL forgotten password command line method to change the password

All prerequisites require root permissions 1. End...

Vue implements weather forecast function

This article shares the specific code of Vue to r...

Vue recursively implements custom tree components

This article shares the specific code of Vue recu...

Solution to MySql Error 1698 (28000)

1. Problem description: MysqlERROR1698 (28000) so...

Free tool to verify that HTML, CSS and RSS feeds are correct

One trick for dealing with this type of error is t...

Implementation of Vue large file upload and breakpoint resumable upload

Table of contents 2 solutions for file upload Bas...

Simple steps to implement H5 WeChat public account authorization

Preface Yesterday, there was a project that requi...