Nginx 是一款高性能的 HTTP 和反向代理服务器,广泛应用于网站和应用程序的部署中。本指南将为您介绍 Nginx 的基本配置和使用方法。

安装 Nginx

首先,您需要在您的服务器上安装 Nginx。以下是使用 apt-get 在 Ubuntu 系统上安装 Nginx 的命令:

sudo apt-get update
sudo apt-get install nginx

基本配置

Nginx 的配置文件位于 /etc/nginx/nginx.conf。以下是配置文件的基本结构:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}

常用配置项

  • server_name: 指定服务器的域名或 IP 地址。
  • listen: 指定服务器的监听端口。
  • root: 指定服务器的根目录。
  • index: 指定默认的页面文件。

虚拟主机

Nginx 支持虚拟主机,可以同时托管多个网站。以下是一个简单的虚拟主机配置示例:

server {
    listen       80;
    server_name  www.example.com;

    location / {
        root   /var/www/example.com;
        index  index.html index.htm;
    }
}

图片展示

Nginx Logo

更多信息

如果您想了解更多关于 Nginx 的信息,请访问我们的 Nginx 学习社区


注意:以上内容仅为示例,实际配置可能因具体需求而有所不同。