How to set up URL link in Nginx server

How to set up URL link in Nginx server

For websites with an architecture like LNMP, they are generally developed based on PHP frameworks. PHP frameworks generally pay attention to elegant links. For example, Laravel, CodeIgniter, ThinkPHP, etc. all support this link mode, which is also called URL rewriting in server configuration. At present, most frameworks use a single entry index.php and then based on the MVC mode, it is generally a request method of /index.php/Model/function. Therefore, if the index.php entry file can be removed, the entire URL will appear simple and beautiful, and the URL mode of websites developed in Python, Java, etc. will be unified. Apache and nginx both support this URL mode. The following is a description of the nginx configuration method.

First, enter the installation directory of nginx. In the previous article, we mentioned that in the configuration file nginx.conf, there is a code segment such as server {} to specify the configuration of a site. There are many other configurations below. We add the following code to the corresponding server {} code segment in the configuration file or the included configuration file to implement URL rewriting:

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  } 

If the original location / {} code already exists, then just append the line of code in the figure, because the configuration of the same route is usually put together; after saving, restart the nginx server to take effect. If nginx is added to the service, you can use /etc/init.d/nginx restart to restart it. After restarting, the URL will be beautified.

There is another way to rewrite the URL on the Internet. Add the following code in the same location to solve it:

location / {
    if (!-e $request_filename) {
      rewrite ^(.*)$ /index.php?s=$1 last;
      break;
    }
  }

After saving, restart nginx to take effect, and you can also achieve the effect of URL beautification. However, according to some articles on the Internet, the first method is recommended for URL beautification.

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:
  • Detailed explanation of configuring Nginx+RTMP+HLS+HTTPFLV server in Ubuntu 18.04 to realize on-demand/live broadcast/recording functions
  • Tutorial on Installing Nginx-RTMP Streaming Server on Ubuntu 14
  • Nginx-rtmp realizes real-time streaming effect of live media
  • Nginx uses nginx-rtmp-module module to realize the live broadcast room function
  • Detailed steps to build nginx+rtmp live server on Mac
  • Detailed explanation of the process of building an image server with nginx (the difference between root and alias)
  • Use Nginx to build a streaming media server to realize live broadcast function
  • How to use nginx to access local static resources on Linux server
  • Nginx builds rtmp live server implementation code

<<:  JavaScript implements the pot-beating game of Gray Wolf

>>:  Descending Index in MySQL 8.0

Recommend

Share JS four fun hacker background effect codes

Table of contents Example 1 Example 2 Example 3 E...

MySQL scheduled full database backup

Table of contents 1. MySQL data backup 1.1, mysql...

Web design dimensions and rules for advertising design on web pages

1. Under 800*600, if the width of the web page is...

Vue elementUI implements tree structure table and lazy loading

Table of contents 1. Achieve results 2. Backend i...

Vue3.0 handwriting magnifying glass effect

The effect to be achieved is: fixed zoom in twice...

InnoDB engine redo file maintenance method

If you want to adjust the size and number of Inno...

Detailed explanation of the setting of background-image attribute in HTML

When it comes to pictures, the first thing we thi...

Press Enter to automatically submit the form. Unexpected discovery

Copy code The code is as follows: <!DOCTYPE ht...

How to configure MySQL on Ubuntu 16.04 server and enable remote connection

background I am learning nodejs recently, and I r...

Detailed explanation of TypeScript's basic types

Table of contents Boolean Type Number Types Strin...

Vue implements a small countdown function

Countdown function needs to be implemented in man...

CSS border half or partially visible implementation code

1. Use pseudo-classes to display half of the Bord...

MYSQL Operator Summary

Table of contents 1. Arithmetic operators 2. Comp...

Explanation of MySQL performance inspection through show processlist command

The show processlist command is very useful. Some...