Quickly switch Nginx as a website front-end proxy server

  

Nginx has been widely used in production environments compared to Apache's high performance and high concurrency features. If the website originally used Apache, how to quickly use Nginx as a front-end proxy server? To provide services?

Configure with a very simple configuration file. Here, abandon complex switching, and many other factors to consider in the production environment, simply explain the implementation method.

Found Nginx configuration file, usually located in /usr/local/nginx/conf, the name is nginx.conf, in order to test, do not change any configuration of Apache, the Nginx service is on port 81.

Find the server configuration and modify it to the following configuration, where website.com is the domain name of the website
server { listen 81; server_name website.com; #charset koi8-r; #access_log logs/host.access .log main; location /{ proxy_pass http://website.com; #root html; #index index.html index.htm; }

For security, we can first test if the configuration file has a syntax error

Execute the following command to test if the syntax is correct
sudo /usr/local/nginx/sbin/nginx -t

If it is correct, it will display something like the following
nginx: the configuration file /usr/Local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Why must I test the configuration file correctly?

1. Each line of the Nginx configuration file has a semicolon after it. Many first-time users will forget to add a semicolon. At this point, some inexplicable errors may occur, such as the prompt missing the brackets. 2.Nginx runs When the configuration file is incorrectly loaded, the process may be uncontrolled. Even if you use the stop command, you cannot stop the process. Therefore, you must first test whether the configuration file is correct (ps: If it is really uncontrolled, then you must force the process to kill.) You can use this command to kill the process sudo killall nginx )

After the configuration file is tested correctly, the reload configuration file can make the configuration take effect
sudo /etc/init.d/nginx reload

The configuration file has been successfully loaded. Open the browser and enter the domain name (original website domain name) + port number 81 of the website configured above. For example, the website.com given in the configuration file can use http://website.com:81 Come visit, then set up Nginx as a front-end proxy server. If it is a Chrome browser, you can open the console, find Network, and see if the Response Header of the first file loaded is already Nginx. Here is my screenshot. You can see that the Server line has become Nginx

< Br>

Copyright © Windows knowledge All Rights Reserved