As a senior CMS website operation personnel, I am well aware that the efficient operation of a content management system is crucial for website operators, especially when it comes to managing multiple sites and SEO optimization.AnQi CMS provides us with a powerful content publishing and management platform with its high-performance and flexible multi-site support developed in Go language.Today, I will introduce in detail how to configure the pseudo-static rules for multi-site AnQiCMS under the Nginx environment, ensuring that your website can enjoy optimized URL structure and smooth access experience.

Deeply Understand AnQiCMS Multi-site and URL Rewriting Mechanism

Before configuring Nginx's pseudo-static rules, we first need to understand the underlying mechanism of AnQiCMS handling multi-site and pseudo-static requests.AnQiCMS as a modern content management system, its 'multi-site management' feature allows us to create and manage multiple independent websites under a single secure CMS application instance.HostDetermine which site the user is visiting by the header, and load the corresponding site's data and content accordingly.

As for 'pseudo-static', AnQiCMS has built-in multiple pseudo-static rule configurations, such as number pattern, model naming pattern, category naming pattern, etc. These rules define the URL structure of website content (such as articles, categories, single pages, etc.) displayed in the browser. For example, the URL of an article may be from/archive.php?id=123Becomes more readable and SEO-friendly/news/article-123.html.It should be emphasized that these pseudo-static rules are parsed and handled by the AnQiCMS application itself.The role played by Nginx here is not to perform complex rewriting of URLs, but rather to forward all requests that cannot be directly matched to static files (such as images, CSS, and JS) to the AnQiCMS application on the backend, where AnQiCMS will handle the subsequent URL parsing and content distribution.

Basics of Nginx reverse proxy configuration

Nginx as a high-performance web server and reverse proxy server is the ideal choice for deploying AnQiCMS.The core function of reverse proxy is to forward the client's requests to the internal application server and return the application server's responses to the client.For AnQiCMS, we usually deploy it on some internal port (such as the default 8001), while Nginx listens on standard HTTP/HTTPS ports (such as 80/443), responsible for receiving external requests and forwarding them to AnQiCMS.

A basic Nginx reverse proxy configuration snippet usually includesproxy_passCommand used to specify the address and port of AnQiCMS application,proxy_set_headerCommand used to correctly transfer the original request header information of the client (especially,HostThe header and client IP address are passed to AnQiCMS, which is crucial for AnQiCMS to identify the correct site and record the user's IP.

The following is a basic Nginx for a single AnQiCMS instance (regardless of how many sites it manages)locationConfiguration example, usually placed in your Nginx site configuration file.serverBlock:

location @AnqiCMS {
    proxy_pass http://127.0.0.1:8001; # 请替换为AnQiCMS实际运行的IP和端口
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    # 更多HTTP头,例如:
    proxy_set_header   X-Forwarded-Proto $scheme;
}

error_page 404 =200 @AnqiCMS; # 当Nginx找不到文件时,将请求转发给AnQiCMS处理

location / {
   try_files $uri $uri/index.html @AnqiCMS; # 尝试查找静态文件,否则转发给AnQiCMS
}

The meaning of this configuration is: Nginx will first try to find the requested URI's corresponding static file in the root directory of the website (usually the AnQiCMSpublicdirectory) orindex.htmlIf found, return the static resource directly. If not found, forward the request to the one named@AnqiCMSinternallocationblock, which will proxy the request tohttp://127.0.0.1:8001(Your AnQiCMS application's listening address and port).error_page 404 =200 @AnqiCMS;Ensure that even 404 errors considered at the Nginx level are also forwarded to AnQiCMS for processing, so that AnQiCMS can handle its internal URL routing or display a custom 404 page.

Configure AnQiCMS backend multi-site

Before configuring multiple site pseudo-statics at the Nginx level, you need to complete the creation of a new site in the AnQiCMS backend.The design of Anqi CMS allows a single application instance to manage multiple independent sites, which greatly simplifies the operation and maintenance work.

In the AnQiCMS backend that is installed and running, new sites are usually added through the 'Multi-site Management' feature. During the process of adding a new site, you need to specify the following key information:

  • Site Name: Used to identify your site.
  • Site Root Directory: This path is the directory used by AnQiCMS to store independent data for this site (such as cache, uploaded files, etc.). It is recommended to use/app/Start with the domain name replaced by underscores, for example/app/dev_anqicms_comEnsure data isolation between different sites.
  • Website addressThe domain name you will use to access the new site, for examplehttp://dev.anqicms.comAnQiCMS will match the request based on this domain.
  • Database nameEach site should have an independent database, and it is also recommended to use domain names with underscores instead of dots, for example,dev_anqicms_com.
  • Administrator account password: Set independent backend management credentials for the new site.
  • Template: Select the template suitable for the new site.

After completing these configurations, AnQiCMS is ready to respond to requests from the new site.

Nginx Reverse Proxy AnQiCMS Multi-site URL Rewrite Rule Configuration

For the multi-site feature of AnQiCMS, there is no need to write different pseudo-static rules for each site in Nginx, because AnQiCMS itself will generate them according toHostIdentify different sites. Nginx needs to do is to configure an independent one for each domain.serverBlock it and forward all requests to the same AnQiCMS application instance.

The following is a complete example of an Nginx multi-site configuration, assuming your AnQiCMS application is running inhttp://127.0.0.1:8001and you havewww.anqicms.comanddev.anqicms.comtwo sites:

"nginx"

Define the upstream server of AnQiCMS application

upstream anqicms_backend {

server 127.0.0.1:8001; # AnQiCMS应用监听的地址和端口
keepalive 64;

}

AnQiCMS public proxy configuration, can be reused

location @AnqiCMS_proxy {

proxy_pass http://anqicms_backend;
proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_set_header   X-Forwarded-Proto $scheme;
proxy_redirect     off;
proxy_buffering    off;
proxy_http_version 1.1;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;

}

First site: www.anqicms.com

server {

listen 80;
server_name www.anqicms.com;
root /www/wwwroot/anqicms/public; # AnQiCMS应用存放静态资源的public目录

#