When deploying AnQiCMS with Baota panel, which folder should the Nginx running directory be configured to?
As an experienced website operations expert, I am happy to provide you with a detailed explanation of the correct configuration method for the Nginx running directory when deploying AnQiCMS using the Baota panel, and to deeply analyze the principles behind it, ensuring that you not only know the what but also the why.
When deploying AnQiCMS using Baota panel, which directory should the Nginx running directory be configured to? Deep analysis and practice
When deploying AnQiCMS such a Go language application using the Baota panel, many operators may be accustomed to directly pointing the Nginx running directory to the root directory of the project.However, for AnQiCMS, the Nginx running directory has specific **practices, which are crucial for the performance, security, and normal operation of the website.
The answer is: The Nginx running directory should be configured to your AnQiCMS project directory.publicFolder.
Now, let's take a detailed look at why it is.publicFolder, as well as how to configure such settings in the Baota panel.
Understand the collaboration mode between AnQiCMS and Nginx.
AnQiCMS is an enterprise-level content management system developed based on the Go language, with core features of high performance and easy deployment.Different from traditional PHP applications (such as WordPress), Go applications usually include a high-performance web server that can directly handle HTTP requests.In this architecture, Nginx plays the role mainlyReverse Proxy (Reverse Proxy)andStatic File Server (Static File Server).
- Reverse Proxy: Nginx receives all user requests and forwards dynamic content requests to the Go application running within AnQiCMS itself (usually listening on port 8001).
- Static Resource Service: The CSS, JavaScript, images, and other static files of AnQiCMS are stored in the project directory by default.
publicIn the folder. To improve the loading speed of the website and alleviate the pressure on the backend Go application, we usually let Nginx directly handle the requests for these static files, rather than forwarding them to the Go application each time.Nginx performs exceptionally well in handling static files, its efficiency is much higher than having the backend application distribute them.
Based on such a collaboration mode, set the Nginx running directory topublicfolder, and cooperate with specific rewrite rules to achieve ** performance and functionality.
Practice of Nginx configuration in Baota panel
The process is roughly as follows when you create a site and configure Nginx for AnQiCMS in the Baota panel:
First, you need to unzip the AnQiCMS installation package to a directory on the server, for example/www/wwwroot/yourdomain.com. In this directory, you will see a file namedpublic.
Next, when creating a new website in the Baota panel, you can choose 'static' or not create a database (because AnQiCMS will connect to the database automatically). The key is the Nginx configuration:
Set website directory In the "Website Directory" tab of the website settings, you need to set the "Running Directory"precisely to the AnQiCMS project directory under your
publicFolderFor example, if the root directory of your AnQiCMS project is/www/wwwroot/yourdomain.comthen the runtime directory should be set to/www/wwwroot/yourdomain.com/publicSo, Nginx will first check this directorypublicLocate the requested file in the directory.Configure Nginx's pseudo-static rules: It is not enough to just set the running directory. Since AnQiCMS is a Go application, its dynamic requests need to be forwarded to the Go program itself.Therefore, you also need to add reverse proxy rules in the "virtual static" configuration of Nginx.Recommended Nginx pseudo-static rules by AnQiCMS (usually in
install.mdorstart.mdAs provided in the document) as follows:location @AnqiCMS { proxy_pass https://en.anqicms.com; # 这里是AnQiCMS应用监听的端口,默认为8001 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 404 =200 @AnqiCMS; # 如果找不到文件,将请求重写到AnqiCMS处理 location / { try_files $uri $uri/index.html @AnqiCMS; # 核心:优先尝试静态文件,否则转发给AnqiCMS }This rule's
location /Paragraph is crucial.try_files $uri $uri/index.html @AnqiCMS;Means:- Nginx will first try to locate in
/www/wwwroot/yourdomain.com/public(which is the runtime directory you set) to find the file requested by the user ($uri). If found, it will return directly, which greatly accelerates the loading of static resources. - If not found
$uriit will try to find$uri/index.html(for example, if the request/aboutit will try to find/public/about/index.html) - If these are not found, Nginx will forward the request
@AnqiCMSThis internal naming rule forwards to the AnQiCMS application running in the backgroundproxy_pass https://en.anqicms.com;) handled by Go application for dynamic routing and content generation.
By such configuration, Nginx can efficiently serve static content and seamlessly pass dynamic content requests to AnQiCMS, ensuring the website is both fast and stable.
- Nginx will first try to locate in
Considerations for multi-site deployment
If you plan to deploy multiple AnQiCMS sites on a single server, this principle also applies.Each AnQiCMS instance (if not installed through the built-in multi-site feature of AnQiCMS, but independently installed) requires a separate port and Nginx configuration.The Nginx running directory of each site should still point to the respective project directory belowpublicFolder, and configure the corresponding pseudo-static reverse proxy rules, justproxy_passThe port to which it points may change depending on the listening port of your AnQiCMS instance.
Summary
Properly configuring the Nginx runtime directory is a key step in the successful deployment of AnQiCMS. Point it to the root directory of the project.publicfolder, with precisetry_filesandproxy_passThe rule can maximize the use of Nginx's static file service advantages and the efficient processing capabilities of Go applications, providing your website with excellent performance and user experience.When operating the Baota panel, be sure to carefully check these configurations to avoid unnecessary running problems.
Frequently Asked Questions (FAQ)
Why can't the Nginx runtime directory be directly set to the root directory of the AnQiCMS project?Answer: If the Nginx runtime directory is directly pointed to the project root directory, Nginx will attempt to directly provide all files under the project root directory, which may expose sensitive information such as code files, configuration files, and so on that should not be publicly disclosed, posing a security risk. At the same time, since the Go application itself handles dynamic requests, Nginx also needs to be configured with reverse proxy to forward requests to the Go application. Directly pointing to the project root directory will cause Nginx to be unable to effectively distinguish between static files and dynamic requests, thus making
try_filesRules are difficult to effectively function, and may instead lead to resource loading errors or security vulnerabilities. The execution directory should be specified accurately topublicThe folder ensures that only static resources meticulously prepared by AnQiCMS are provided directly by Nginx, while the remaining dynamic requests are handled by the Go application, achieving a balance of security and performance.In Nginx configuration
try_filesWhat role does the command play here?Answer:try_files $uri $uri/index.html @AnqiCMS;It is a very powerful directive in Nginx configuration that defines the priority order of Nginx handling requests.$uri: Nginx will first try to find the file that matches the request URI in the running directory you have configured (i.e./publicfolder). If the user requests/css/style.css, Nginx will try to locate/public/css/style.css.$uri/index.htmlIf the first attempt fails, Nginx will then try to find in the subdirectory corresponding to the URIindex.htmlfile. For example, if the request/about/, Nginx will try to locate/public/about/index.html.@AnqiCMSIf both of these attempts fail, it means that the request is not a static file or the corresponding default file cannot be found, then Nginx will pass the request to the one namedAnqiCMSThis block processes the internal naming Location block, which will pass throughproxy_passThe instruction forwards the request to the Go application running in the background. In short,try_filesEnsured that Nginx can serve static resources with priority and efficiency, only forwarding requests to dynamic applications when static resources are not found, thereby optimizing website performance.
How should I modify the Nginx configuration if my AnQiCMS application does not listen on port 8001?Answer: AnQiCMS listens on port 8001 by default, but you may have changed this port in some cases (for example, in
config.jsonMake changes)。If your AnQiCMS application is actually listening on a different port (such as 8002), then you need to modify the pseudo-static configuration in Nginx.proxy_passInstruction, point it to the correct port. Specifically, set `proxy_passhttp://127.0.0.1: