How to configure AnQiCMS's Nginx proxy in the Linux command line environment?
As a senior CMS website operation personnel in an enterprise, I know that stable and efficient deployment is the foundation of successful website operation.Under Linux, Nginx, as a high-performance reverse proxy server, is an ideal partner for AnQiCMS, which can effectively improve the speed and security of website access.I will give a detailed introduction on how to configure Nginx under the Linux command line environment to provide a stable and reliable reverse proxy service for AnQiCMS.
Configure AnQiCMS's Nginx proxy in the Linux command-line environment
AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, known for its ease of deployment, fast execution speed, and SEO-friendly features, making it the preferred choice for many small and medium-sized enterprises and content operation teams.When running AnQiCMS on a Linux server, it is usually necessary to use Nginx for reverse proxying so that users can access the website through standard ports 80 or 443 and enjoy the benefits of Nginx such as static file acceleration and SSL encryption.
Preparation: Make sure AnQiCMS is already running on the server
Before configuring Nginx, you need to make sure that the AnQiCMS program has been downloaded and successfully run on your Linux server.Generally, AnQiCMS listens on a specific port, such as the default port 8001.You can start AnQiCMS by following these steps:
First, download the installation package for Linux from the AnQiCMS official website. Extract it to the directory you choose, for example/www/wwwroot/yourdomain.com/In this directory, you will find the AnQiCMS executable file (usually namedanqicms) and onestart.shscript.
To ensure that AnQiCMS can also run automatically after the server restarts, it is recommended to add it to the system's scheduled tasks (Crontab). By runningcrontab -eCommand to open the Crontab editor and then add the following lines:
*/1 * * * * /www/wwwroot/yourdomain.com/start.sh
Be sure to set/www/wwwroot/yourdomain.com/Replace with the actual installation path of AnQiCMS. This command will check every minute if AnQiCMS is running, and if not, it will start it to ensure the continuous availability of the service.After adding, please execute manually once./start.shCommand to ensure that the AnQiCMS service is running.
Configure Nginx reverse proxy
Next, we will configure Nginx to receive user requests and forward them to the port listened to by AnQiCMS. The Nginx configuration file is usually located/etc/nginx/sites-available/or/etc/nginx/conf.d/Under the directory. You need to create a new configuration file for your AnQiCMS site, for exampleyourdomain.com.conf.
Use your favorite text editor (such asviornanoCreate and edit this file:
sudo nano /etc/nginx/sites-available/yourdomain.com.conf
Then, paste the following Nginx configuration code into the file. Modify it according to your actual situation.server_nameandrootcommand.
server
{
listen 80; # 监听标准的HTTP端口
# listen 443 ssl http2; # 如果您配置了SSL证书,请取消注释并配置SSL相关指令
server_name yourdomain.com www.yourdomain.com; # 您的网站域名,可以添加多个
# 网站根目录,指向AnQiCMS安装目录下的public文件夹
root /www/wwwroot/yourdomain.com/public;
# 配置Nginx反向代理,将请求转发给AnQiCMS
location @AnqiCMS {
proxy_pass https://en.anqicms.com; # AnQiCMS监听的端口,默认为8001
proxy_set_header Host $host; # 转发原始请求的Host头
proxy_set_header X-Real-IP $remote_addr; # 转发客户端的真实IP地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 转发客户端代理链的IP地址
# proxy_set_header X-Forwarded-Proto $scheme; # 如果使用SSL,请取消注释
}
# 处理404错误,将所有未找到的请求路由到AnQiCMS处理
error_page 404 =200 @AnqiCMS;
# 尝试查找文件,如果找不到则交给AnQiCMS处理
location / {
try_files $uri $uri/index.html @AnqiCMS;
}
# 设置访问日志,您可以指定日志文件的路径
access_log /var/log/nginx/yourdomain.com_access.log;
error_log /var/log/nginx/yourdomain.com_error.log;
}
Configuration details:
listen 80;: Nginx listens to the standard port for HTTP requests. If you want to enable HTTPS, you also need to configurelisten 443 ssl http2;and add the SSL certificate path and related configurations.server_name yourdomain.com www.yourdomain.com;: Specify your website domain. When users access these domains, Nginx will apply this configuration.root /www/wwwroot/yourdomain.com/public;This is a very critical step. It points the Nginx website root directory to the AnQiCMS installation directory.publicFolder. AnQiCMS static resources (such as CSS, JS, images, etc.) are usually stored in this directory, Nginx can directly serve these static files without going through the AnQiCMS program.location @AnqiCMS { ... }: This is a named location block used to define how to proxy requests to AnQiCMS.proxy_pass https://en.anqicms.com;: This instruction is the core, it tells Nginx to forward all matched requests to the local@AnqiCMSserver.8001Port, which is the address that the AnQiCMS service listens on. If you have modified the running port of AnQiCMS, please make the corresponding modification here.proxy_set_header ...: These instructions are used to pass the original client request header information (such as Host, real IP, etc.) to AnQiCMS, which is crucial for AnQiCMS to obtain the correct client information and log records.
error_page 404 =200 @AnqiCMS;When Nginx tries to locate a file but returns a 404 error, it will internally redirect the request to@AnqiCMSThis means that AnQiCMS will be responsible for handling all missing URLs, achieving friendly URL structures and pseudo-static pages.location / { try_files $uri $uri/index.html @AnqiCMS; }: This is the default request handling block. It first attempts to findrootin the directory a file that matches the request URI orindex.htmlIf found, return directly. If the static file does not exist, forward the request to@AnqiCMSName the location block for processing, so that content is dynamically generated by AnQiCMS.
After saving and closing the configuration file, you need to create a symbolic link to activate the configuration file in Nginx'ssites-enableddirectory:
sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/
Check Nginx configuration and restart
Before restarting the Nginx service, make sure to check the syntax of the configuration file to avoid service startup failure:
sudo nginx -t
If the output displayssyntax is okandtest is successfulThis indicates that the configuration is correct. Now, you can reload the Nginx service to apply the new configuration:
sudo systemctl reload nginx
# 或者
sudo service nginx reload
Verify deployment
After completing the Nginx configuration and restarting the service, you can enter your domain name in the browser (for examplehttp://yourdomain.com) Visit the AnQiCMS website. If everything is configured correctly, you will see the AnQiCMS initialization installation interface or the content of the deployed website.
By following these steps, you have successfully configured Nginx reverse proxy for AnQiCMS under the Linux command-line environment, providing a high-performance, highly available access entry for your website.
Frequently Asked Questions (FAQ)
1. Why does the website's CSS, JS, or images fail to load normally after configuring Nginx reverse proxy?
This is usually due to Nginx'srootThe command configuration is incorrect, or the static file path of AnQiCMS does not match the expected Nginx. Please ensure that the Nginx configuration containsrootThe command precisely points to the AnQiCMS installation directory underpublicFolder, for exampleroot /www/wwwroot/yourdomain.com/public;. Nginx will directly serve static resources from this directory.In addition, also check whether the "Global Settings" in AnQiCMS backend is correctly set for "Website Address" and "Template Static File Address", ensuring they are consistent with the domain and path after Nginx proxy.
2. I want to run multiple AnQiCMS sites on the same server, how should I configure Nginx?
AnQiCMS supports multi-site management, you only need one AnQiCMS core program to run multiple sites. At the Nginx level, you need to create an independent site for each AnQiCMS site.serverBlock. Eachserverblock'sserver_nameThe command should correspond to different domain names, and each site shouldrootThe command still points to the same AnQiCMS core programpublicDirectory. AnQiCMS will respond to the requestedHostHeader (Nginx through)proxy_set_header Host $host;Passing) to identify which site is being accessed and to present the corresponding content.In the multi-site management of AnQiCMS backend, you need to add a site configuration for each new domain and ensure that the database and root directory settings are correct.
3. If the default AnQiCMS port 8001 is occupied, how can I solve it?
When AnQiCMS fails to start and prompts that the port is occupied, you can choose to change the listening port of AnQiCMS. This is usually done by editing the AnQiCMS installation directory underconfig.jsonImplement a file. FindportField and change it to another unused port, such as8002. After changing, you need to restart the AnQiCMS service and update the Nginx configuration file accordinglyproxy_passthe port number of the command, for example, toproxy_pass https://en.anqicms.com;is modified toproxy_pass http://127.0.0.1:8002;. After updating the Nginx configuration, don't forget to check the syntax and reload the Nginx service.