nginx study notes 1 Use nginx to build a simple HTTP server
nginx, as a lightweight http server, can handle high concurrent http requests well. At the same time, it can also be configured as an http proxy server. As a newbie in backend development, with such an excellent open source server in front of me, I have to do some research and learn something to be reliable!
Of course, the first step in learning nginx is to learn how to use it.
1 Install nginx
First, download the source code from the official website http://nginx.org/download/nginx-1.8.0.tar.gz, unzip it and enter the nginx-1.8.0 directory.
nginx relies on the pcre library, so you also need to download the pcre source code.
Extract the pcre source code to nginx-1.8.0/3rdparty/pcre-8.32, and then execute
<code>./configure --with-http_ssl_module --with-pcre=./3rdparty/pcre-8.32/ ./make ./sudo make install </code>
in the root directory to complete the installation. The default installation directory is /usr/local/nginx.
During the installation process, you may be prompted that the openssl library is missing. You can directly apt-get install.
2 Configure nginx
Before using nginx, first create an nginx directory under /home/bookxiao/ to store resource files, and then create soft connections to conf, sbin and logs under /usr/local/nginx to avoid Switch directories frequently.
<code>bookxiao<span>@ubuntuforfun</span><span>:~/nginx</span><span>$ </span>ls bin conf data logs</code>
nginx’s configuration file is located under conf/ and is named nginx.conf. The basic unit that makes up the configuration file is a piece of "directives". The instructions consist of three parts: name, parameter attributes, and the ':' at the end. In addition, some instructions will be surrounded by { } to form a block; if the block contains some instructions, then the block is also called a "context".
All instructions in the configuration file are either within a certain context or not included in any context (that is, located in the main context), which is somewhat similar to global variables in C.
For example, a simple configuration file looks like this:
<code>bookxiao<span>@ubuntuforfun</span><span>:~/nginx/conf</span><span>$ </span>cat nginx.conf <span>#user nobody;</span> worker_processes <span>1</span>; events { worker_connections <span>1024</span>; } http { server { listen <span>80</span>; server_name localhost; location / { root html; index index.html index.htm; } location /images { root /home/bookxiao/nginx/data; } } }</code>
event, http, server are all "context"; the instruction "worker_processes 1;" is located in the main context.
To simply test the functionality of nginx, first take a look at how to configure the server{} block. In nginx, each server block represents an HTTP service, and different servers have different ports and server_names.
Suppose we now have an image, and the storage path is ~/nginx/data/images/nginx study notes 1 Use nginx to build a simple HTTP server.jpg.
Now we want to enter http://127.0.0.1/images/nginx study notes 1 Use nginx to build a simple HTTP server.jpg
in the browser, it will look like this:
Look carefully at the configuration file above and see that there is a location The block is defined like this:
<code> location /images { root /home/bookxiao/nginx/<span><span>data</span>;</span> }</code>
The "/images" followed by locations is used to prefix match the client's URL request. If it matches, then the root value is added to the front end of the URL to form a new URL to go to the file. The system looks for resource files.
For example, the original URL here is "/images/nginx study notes 1 Use nginx to build a simple HTTP server.jpg", then the final URL is "/home/bookxiao/nginx/data/images/nginx study notes 1 Use nginx to build a simple HTTP server.jpg".
What if there are multiple locations matching a certain URL? The strategy of nginx is to select the location with the longest matching value.
So, we can add such a server:
<code> server { listen <span>9999</span>; server_name localhost_test; location /images { root /home/bookxiao/nginx/<span><span>data</span>/test;</span> } }</code>
and then execute sudo nginx -s reload
to reload the configuration file. Then we create a new test/images/ directory under data/. In this way, we enter http://127.0.0.1:9999/images/nginx study notes 1 Use nginx to build a simple HTTP server.img
in the browser. Then we will see:
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces nginx study notes 1. Use nginx to build a simple HTTP server, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.
