- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
What is Nginx?
Nginx (pronounced "Engine-X") is a powerful, open-source web server software that can also function as a reverse proxy, load balancer, and HTTP cache. It’s known for its high performance, stability, and low resource usage, making it a popular choice for hosting websites and web applications.
Why Use Nginx?
Web Server
Nginx serves static content (like HTML, CSS, and images) to users’ browsers. It can also handle dynamic content by passing requests to application servers (e.g., those running PHP, Python, or Node.js).
Reverse Proxy
As a reverse proxy, Nginx forwards client requests to backend servers, distributing the load and improving security by hiding the backend infrastructure.
Load Balancer
Nginx can distribute incoming traffic across multiple servers to prevent any single server from becoming overwhelmed, ensuring reliability.
Configuration
Nginx uses a simple, text-based configuration file (usually nginx.conf) to define how it handles requests. This file is typically located in /etc/nginx/ on Linux systems.
Getting Started with Nginx
Installation
Here’s how to install Nginx on a Linux system (Ubuntu/Debian):
1. Update the package list:
sudo apt update
2. Install Nginx:
sudo apt install nginx
3. Start Nginx and enable it to run on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
4. Verify Nginx is running by visiting http://your_server_ip in a browser. You should see the default Nginx welcome page.
Basic Configuration
The main configuration file is /etc/nginx/nginx.conf. A simple configuration might look like this:
http {
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
}
1. Create a directory for your website:
sudo mkdir -p /var/www/mywebsite
2. Create a basic index.html file:
echo "<h1>Hello from Nginx!</h1>" | sudo tee /var/www/mywebsite/index.html
3. Update the Nginx configuration (/etc/nginx/sites-available/mywebsite):
server {
listen 80;
server_name mywebsite.com;
root /var/www/mywebsite;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
4. Enable the site by linking it to sites-enabled:
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
5. Test the configuration for errors:
sudo nginx -t
6. Reload Nginx to apply changes:
sudo systemctl reload nginx
7. Visit http://your_server_ip to see your “Hello from Nginx!” page.
Common Commands
Nginx (pronounced "Engine-X") is a powerful, open-source web server software that can also function as a reverse proxy, load balancer, and HTTP cache. It’s known for its high performance, stability, and low resource usage, making it a popular choice for hosting websites and web applications.
Why Use Nginx?
Speed: Nginx is designed to handle many simultaneous connections efficiently.
Scalability: It can manage high-traffic websites with ease.
Flexibility: Works as a web server, reverse proxy, or load balancer.
Simplicity: Configuration files are straightforward and easy to understand.
Community: A large community and plenty of documentation are available.
Web Server
Nginx serves static content (like HTML, CSS, and images) to users’ browsers. It can also handle dynamic content by passing requests to application servers (e.g., those running PHP, Python, or Node.js).
Reverse Proxy
As a reverse proxy, Nginx forwards client requests to backend servers, distributing the load and improving security by hiding the backend infrastructure.
Load Balancer
Nginx can distribute incoming traffic across multiple servers to prevent any single server from becoming overwhelmed, ensuring reliability.
Configuration
Nginx uses a simple, text-based configuration file (usually nginx.conf) to define how it handles requests. This file is typically located in /etc/nginx/ on Linux systems.
Getting Started with Nginx
Installation
Here’s how to install Nginx on a Linux system (Ubuntu/Debian):
1. Update the package list:
sudo apt update
2. Install Nginx:
sudo apt install nginx
3. Start Nginx and enable it to run on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
4. Verify Nginx is running by visiting http://your_server_ip in a browser. You should see the default Nginx welcome page.
Basic Configuration
The main configuration file is /etc/nginx/nginx.conf. A simple configuration might look like this:
http {
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
}
listen 80: Nginx listens for HTTP requests on port 80.
server_name: Specifies the domain name (e.g., example.com).
location /: Defines how to handle requests to the root URL.
root: Sets the directory where website files are stored.
index: Specifies the default file to serve (e.g., index.html).
1. Create a directory for your website:
sudo mkdir -p /var/www/mywebsite
2. Create a basic index.html file:
echo "<h1>Hello from Nginx!</h1>" | sudo tee /var/www/mywebsite/index.html
3. Update the Nginx configuration (/etc/nginx/sites-available/mywebsite):
server {
listen 80;
server_name mywebsite.com;
root /var/www/mywebsite;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
4. Enable the site by linking it to sites-enabled:
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
5. Test the configuration for errors:
sudo nginx -t
6. Reload Nginx to apply changes:
sudo systemctl reload nginx
7. Visit http://your_server_ip to see your “Hello from Nginx!” page.
Common Commands
Start Nginx: sudo systemctl start nginx
Stop Nginx: sudo systemctl stop nginx
Restart Nginx: sudo systemctl restart nginx
Reload Configuration: sudo systemctl reload nginx
Check Status: sudo systemctl status nginx
Test Configuration: sudo nginx -t