In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the Nginx installation and configuration rules". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn what the Nginx installation and configuration rules are.
1. Nginx installation and operation (mac os environment)
1. Install nginx
It can be installed directly through homebrew:
$brew install nginx
After installation, the default home page file is in the / usr/local/var/www folder
The default profile address is / usr/local/etc/nginx/nginx.conf
Nginx uses port 8080 by default, and if you find that the port is occupied (check the port occupancy through $lsof-ikill 8080), you can kill the process using that port ($kill process pid). Or modify the default port of nginx (/ usr/local/etc/nginx/nginx.conf)
two。 Start nginx
$brew services start nginx
Or go to the directory / usr/local/bin $. / nginx
After the startup is successful, the browser accesses http://localhost:8080/ and you can see the static resources returned by the nginx server (resource / usr/local/var/www/index.html by default)
3. Stop nginx
$nginx-s stop
4. Restart nginx
$nginx-s reload
5. View nginx configuration path information
$brew info nginx II. Nginx rule configuration
More configurations can be viewed
1. Location
2. Root and alias
In nginx, you can specify the access path to resources through root and alias.
1) root:
Location / {root / usr/local/var/www/; index index.html index.htm;}
The above rule: when requesting the address http://localhost:8080/index.html, the resources accessed are: / 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 address of the accessed resource is actually the path specified by root + the path to which location matches.
2) alias:
Alias is an alias, which is slightly different from the matching rules of root.
Location / a/ {alias / usr/local/var/www/b/;}
The above rule: when requesting the address http://localhost:8080/a/, the resources accessed are: / 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.
Alias must end with "/", otherwise the file will not be found, and root is optional to "/".
3. Try_file
Location / test/ {try_files $uri $uri/ / a Universe 1.png;}
Try_files tries to read the files accessed by the user in the website directory, and returns directly if the first variable exists; if it does not exist, it continues to read the second variable, and if it exists, it returns directly; if it does not exist, it jumps to the third parameter.
Uri is a variable of nginx that holds the address accessed by the user. For example, to visit http://www.xxx.com/index.html,\ $uri is / index.html.
$uri/ represents access to a directory, such as http://www.xxx.com/hello/test/, so\ $uri/ is / hello/test/.
For example, the above rule: when requesting the address of http://localhost:8080/test/2.png, try_files will determine whether it is a file or a directory, only to find that it is a file, which matches the first parameter $uri variable. Then go to the website directory to find out if the test/2.png file exists, and if so, read it back directly. If it does not exist, jump to the third parameter, which returns the site root + / a/1.png file (/ usr/local/var/www/a/1.png).
More usage:
4. Rewrite
The rewrite function is to implement url rewriting and redirection.
Syntax rewrite regex replacement [flag]
Rewrite can only be placed in server {}, location {}, if {}, and can only work on strings after the domain name except for the passed parameters. For example, http://www.xxx.com/a/b/index.html?param=1&u=str only overrides / a/b/index.html.
The order in which rewrite is executed:
Execute rewrite instructions for server blocks
Perform location matching
Execute the rewrite instruction in the selected location
Flag flag bit:
Last: equivalent to the [l] tag of apache, indicating that the rewrite is complete
Break: stops executing the subsequent rewrite instruction set of the current virtual host
Redirect: returns 302 temporary redirection. The address bar will display the address after the jump.
Permanent: returns 301 permanent redirection, and the address bar will display the address after the jump.
Location / home/ {rewrite ^ / home/test/ http://www.baidu.com;}
The above rule: when you visit the address of http://localhost:8080/home/test/, the page will be redirected to http://www.baidu.com.
Some little tips:
How can nginx redirect url without changing the display of url in the browser?
Proxy_pass can specify reverse proxy
More usage:
Third, some command line configuration (mac os)
1. How to open a file with vscode on the command line
Cd / usr/local/bin/ln-s "/ applications/visual studio code.app/contents/macos/electron" vscode
Where / applications/visual studio code.app/contents/macos/electron is the executable file of vscode, and the ln-s command puts it into the / usr/local/bin/ directory through a soft connection. This allows you to open the file with the vscode command elsewhere on the command line.
Thank you for your reading, the above is the content of "what are the Nginx installation and configuration rules". After the study of this article, I believe you have a deeper understanding of what the Nginx installation and configuration rules are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.