Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Nginx module

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to solve the problem of Nginx module". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem of Nginx module".

Problem recurrence

After hearing the feedback from the operation, I quickly logged on to the server to troubleshoot the problem. First, check that the startup process of the interface service is normal. Verify that the ip and port of the interface service are normal, and the result is no problem. Next, the request was forwarded through Nginx, and there was a problem and the interface could not be accessed. At the same time, the following log information is output in Nginx's access.log file.

192.168.175.120-[26/Feb/2021:21:34:21 + 0800] "GET / third/system/base/thirdapp/get_detail HTTP/1.1" 404 0 "http://192.168.175.100/api/index.html"" Mozilla/5.0 (Windows NT 10.0; Win64; x64 Rv:85.0) Gecko/20100101 Firefox/85.0 "192.168.175.120-- [26/Feb/2021:21:34:22 + 0800]" GET / third/system/base/thirdapp/get_detail HTTP/1.1 "404 0" http://192.168.175.100/api/index.html"Mozilla/5.0 (Windows NT 10.0; Win64; x64 Rv:85.0) Gecko/20100101 Firefox/85.0 "192.168.175.120-- [26/Feb/2021:21:34:26 + 0800]" GET / third/system/base/thirdapp/get_detail HTTP/1.1 "4040" http://192.168.175.100/api/index.html"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0"

At this point, it is found from the Nginx log that the status of the output is 404 and the back-end interface service is not found. In order to further locate the problem, I directly access the interface service through the curl command on the online environment, and the result is normal.

After this series of operations, we can be sure that the problem lies with Nginx.

Analysis of problems

Nginx opens the debug module

Now that we have located the problem, we need to analyze the specific cause of the problem. Since it's Nginx, the first thing that comes to mind is to debug Nginx to find the cause of the error. So I entered the following command on the server command line to see the configuration when Nginx was installed.

Nginx-V

Note: the system environment variable has been configured for Nginx. If you do not configure the system environment variable, you need to enter the full path of the directory where the nginx command is located, for example:

/ usr/local/nginx/sbin/nginx-v

The command line outputs the following information.

Configure arguments:-prefix=/usr/local/nginx-- with-http_stub_status_module-- add-module=/usr/local/src/fastdfs/fastdfs-nginx-module-1.22/src-- with-openssl=/usr/local/src/openssl-1.0.2s-- with-pcre=/usr/local/src/pcre-8.43-- with-zlib=/usr/local/src/zlib-1.2.11-- with-http_ssl_module

As you can see, Nginx's debug module is not configured when Nginx is installed.

So I found the installation file for Nginx on the server and recompiled Nginx by typing the following command on the command line.

Cd / usr/local/src/nginx/ # enter the root directory of the Nginx installation file make clean # to clear the compilation information. / configuration-prefix=/usr/local/nginx-1.17.8-with-http_stub_status_module-add-module=/usr/local/src/fastdfs/fastdfs-nginx-module-1.22/src-with-openssl=/usr/local/src/openssl-1.0.2s-with-pcre=/ Usr/local/src/pcre-8.43-- with-zlib=/usr/local/src/zlib-1.2.11-- with-http_ssl_module-- with-debug # set configuration information for compiling Nginx make # compiling Nginx Remember not to enter make install

In the above command, remember not to enter make install to install.

After executing the make command, the nginx command will be generated in the objs directory of the current directory. In this case, we need to stop the Nginx service, back up the nginx command in the / usr/local/nginx/sbin/ directory, copy the nginx command in the objs directory to the / usr/local/nginx/sbin/ directory, and then start the Nginx service.

Nginx_service.sh stop # stop the Nginx service mv / usr/local/nginx/sbin/nginx / usr/local/nginx/sbin/nginx.bak # backup the original nginx command cp. / objs/nginx / usr/local/nginx/sbin/nginx # copy the nginx command nginx_service.sh start # start the Nginx service through the script

Note: here, this Nginx has been removed from the access layer gateway before stopping the Nginx service, so it will not affect the online environment. To avoid problems in restarting Nginx with the newly compiled nginx command, stop the Nginx service by script, then copy the nginx command, and then start the Nginx service.

Configure Nginx output debug log

Configure the following information in the nginx.conf file of Nginx.

Error_log logs/error.log debug

At this point, the debug log function of Nginx is turned on, and the debug information is output to the error.log file.

Analyze the problem

Next, enter the following command on the server command line to listen to the output log of the error.log file.

Tail-F / usr/local/nginx/logs/error.log

Then simulate access to the http interface, and you can see the following information output in the error.log file.

21:34:26 on 2021-02-26 [debug] 31486 / 0: * 56 http request line: "GET / third/system/base/thirdapp/get_detail HTTP/1.1" 2021-02-26 21:34:26 [debug] 31486 / 0: * 56 http uri: "/ third/system/base/thirdapp/get_detail" 2021-02-26 21:34:26 [debug] 31486 / 0: * 56 http args: "" 21:34:26 on 2021-02-26 [ Debug] 31486: * 56 http exten: "" 21:34:26 on 2021-02-26 [debug] 31486 / 0: * 56 posix_memalign: 0000000000FF6450:4096 @ 16 2021-02-26 21:34:26 [debug] 31486 / 0: * 56 http process request header line 2021-02-26 21:34:26 [debug] 31486 / 0: * 56 http header: "Host: 10.31.5.66" 2021-02-26 21:34:26 [debug] 31486 / 0 : * 56 http header: "User-Agent: Mozilla/5.0 (Windows NT 10.0 Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0 "2021-02-26 21:34:26 [debug] 31486 / 0: * 56 http header:" Accept: * / * "2021-02-26 21:34:26 [debug] 31486 / * 56 http header:" Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en Qroom0.2 "2021-02-26 21:34:26 [debug] 31486 http header: * 56 http header:" Accept-Encoding: gzip Deflate "2021-02-26 21:34:26 [debug] 31486: * 56 http header:" Referer: http://192.168.175.100/api/index.html" 2021-02-26 21:34:26 [debug] 31486: * 56 http header: "Connection: keep-alive" 2021-02-26 21:34:26 [debug] 31486: 0: * 56 http header done 2021-02-26 21:34:26 [debug] 31486: * 56 rewrite phase: 0 2021-02-26 21:34:26 [debug] 3148620: * 56 test location: "/" 2021-02-26 21:34:26 [debug] 31486500: * 56 test location: "file/" 2021-02-26 21:34:26 [debug] 31486400: * 56 test location: ~ "/ base" 2021-02-26 21:34:26 [debug] 3148620th: * 56 using configuration "/ base"

From the output log above, we can see that the interface address accessed is "/ third/system/base/thirdapp/get_detail", as shown below.

21:34:26 on 2021-02-26 [debug] 3148640: * 56 http uri: "/ third/system/base/thirdapp/get_detail"

When Nginx forwards, it matches "/", "file/", "~ / base" respectively, and finally forwards the request to "/ base", as shown below.

21:34:26 on 2021-02-26 [debug] 31486: * 56 test location: "/" 2021-02-26 21:34:26 [debug] 31486 / 0: * 56 test location: "file/" 2021-02-26 21:34:26 [debug] 31486 / 0: * 56 test location: ~ "/ base" 2021-02-26 21:34:26 [debug] 31486 * 0: * 56 using configuration "/ base"

Let's take a look at the configuration of Nginx, open the nginx.conf file and find the following configuration.

Location ~ / base {proxy_pass http://base; proxy_set_header Host $host:$server_port;} location ~ / third {proxy_pass http://third; proxy_set_header Host $host:$server_port;}

So the question is, the access interface is obviously "/ third/system/base/thirdapp/get_detail", why do you go under "/ base"?

Speaking of which, I believe that the careful partner has found the problem, yes, it is the pot of operation and maintenance again!

Solve the problem

After reading the configuration of Nginx, I believe that many partners should know how to solve the problem, and that is to configure the following in nginx.conf.

Location ~ / base {proxy_pass http://base; proxy_set_header Host $host:$server_port;} location ~ / third {proxy_pass http://third; proxy_set_header Host $host:$server_port;}

Modify it as shown below.

Location / base {proxy_pass http://base; proxy_set_header Host $host:$server_port;} location / third {proxy_pass http://third; proxy_set_header Host $host:$server_port;}

Just remove the "~" symbol.

Next, once again simulate access to the http interface, which can access the interface normally.

Next, turn off the debug function of Nginx, that is, comment out the error_log logs/error.log debug; configuration in the nginx.conf file, as shown below.

# error_log logs/error.log debug

Reload the nginx.conf file.

Nginx_service.sh reload

Finally, the problem is solved by adding Nginx to the access layer gateway.

Forwarding rules of popular Science Nginx

Location syntax of Nginx

Location [= | ~ | ~ * | ^ ~] / uri/ {… }

= a strict match. If the request matches the location, the search will be stopped and the request will be processed immediately

~ case-sensitive matching (regular expressions are available)

~ * case-insensitive matching (regular expressions are available)

! ~ case-sensitive mismatch

! ~ * case-insensitive mismatch

^ ~ if you use this prefix for a regular string, tell nginx not to test the regular expression if the path matches

Example 1:

Location / {}

Match any request

Example 2:

Location ~ *. (gif | jpg | jpeg) ${rewrite. (gif | jpg | jpeg) $/ logo.png;}

Matches any request that ends in gif, jpg, jpeg without case sensitivity and redirects the request to the / logo.png request

Example 3:

Location ~ ^. +\ .txt ${root / usr/local/nginx/html/;}

Case-sensitive matches requests that end in .txt and sets the path to this location as / usr/local/nginx/html/. That is, requests ending in .txt will access the txt file under the / usr/local/nginx/html/ path

The difference between alias and root

The root actual access file path splices the path in URL

The actual access file path of alias will not splice the path in URL

Examples are as follows:

Location ^ ~ / binghe/ {alias / usr/local/nginx/html/binghetic/;}

Request: http://test.com/binghe/binghe1.html

Actual access: / usr/local/nginx/html/binghetic/binghe1.html file

Location ^ ~ / binghe/ {root / usr/local/nginx/html/;}

Request: http://test.com/binghe/binghe1.html

Actual access: / usr/local/nginx/html/binghe/binghe1.html file

The difference between last and break keywords

(1) when last and break appear outside location, their functions are the same without any difference.

(2) when last and break appear inside the location:

Last uses the last instruction. After rewrite, it will jump out of the scope of location and start again.

Break uses the break directive. After rewrite, it does not jump out of the scope of location, and its entire life cycle is in the current location.

The difference between permanent and redirect keywords

Rewrite... Permanent is permanently redirected. The status code in the request log is 301.

Rewrite... Redirect temporary redirection. The status code in the request log is 302.

Comprehensive example

Redirect URL that conforms to a regular expression to a fixed page

For example, we need to redirect the URL that corresponds to the regular expression "/ test/ (\ d +) / [\ wmur.] +" to a fixed page. Pages that conform to this regular expression may be: http://test.com/test/12345/abc122.html, http://test.com/test/456/11111cccc.js, etc.

As you can see from the above introduction, we can use rewrite redirection or the alias keyword to achieve our goal here. So here's what you can do:

(1) use the rewrite keyword

Location ~ ^. +\ .txt ${root / usr/local/nginx/html/;} location ~ * ^ / test/ (\ d +) / [\ w -\.] + ${rewrite ^ / test/ (\ d +) / [\ w -\.] + $/ testpage.txt last;}

Here all eligible URL (PS: case-insensitive) are redirected to the / testpage.txt request, that is, the / usr/local/nginx/html/testpage.txt file

(2) use the alias keyword

Location ~ * ^ / test/ (\ d +) / [\ w -\.] + ${alias / usr/local/nginx/html/binghetic/binghe1.html;}

Here all eligible URL (case-insensitive) are redirected to the / usr/local/nginx/html/binghetic/binghe1.html file

Thank you for your reading, the above is the content of "how to solve the problem of Nginx module", after the study of this article, I believe you have a deeper understanding of how to solve the problem of Nginx module, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report