Nginx 安装与配置规则入门详解

下面是 Nginx 安装与配置规则的完整攻略:

Nginx 安装与配置规则入门详解

一、介绍

Nginx 是一款开源的高性能 Web 服务器软件,它可以作为反向代理、负载均衡服务器和 HTTP 缓存服务器。本文将介绍如何在 Linux 系统上安装并配置 Nginx,并给出两个实际应用的示例。

二、安装 Nginx

2.1 在 Ubuntu 系统上安装 Nginx

在 Ubuntu 系统上,可以使用 apt-get 命令来安装 Nginx。具体方法如下:

sudo apt-get update
sudo apt-get install nginx

安装完成后,使用以下命令检查 Nginx 是否已经启动:

systemctl status nginx

若 Nginx 已经启动,会出现如下信息:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-01-07 14:11:54 CST; 1h 19min ago

2.2 在 CentOS 系统上安装 Nginx

在 CentOS 系统上,可以使用 yum 命令来安装 Nginx。具体方法如下:

yum install nginx

安装完成后,使用以下命令检查 Nginx 是否已经启动:

systemctl status nginx.service

若 Nginx 已经启动,会出现如下信息:

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           └─php-fpm.conf
   Active: active (running) since Thu 2022-01-06 23:29:38 CST; 11h ago

三、配置 Nginx

3.1 修改默认网站设置

Nginx 默认的网站设置在 /etc/nginx/sites-available/default 文件中。打开该文件,修改以下参数:

  1. 将默认监听端口改为 80
listen 80 default_server;
  1. 修改默认网站目录:
root /var/www/html;
  1. 将原始的欢迎页面注释掉:
#index index.html index.htm index.nginx-debian.html;

将注释去掉的原始的欢迎页面可以是自己的网站首页。

3.2 新建虚拟主机

为了支持多个网站,需要在 Nginx 中创建一个新的虚拟主机。可以按照以下步骤进行:

  1. /etc/nginx/sites-available/ 目录下新建一个 .conf 文件,例如:
sudo vim /etc/nginx/sites-available/example.com.conf
  1. 编辑 .conf 文件,添加以下内容:
server {
    listen 80;
    server_name example.com www.example.com;
    access_log /var/log/nginx/example.com.access.log;

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

其中,example.com 为自己的域名或者 IP。

  1. /etc/nginx/sites-enabled/ 目录下创建一个软链接:
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
  1. 重启 Nginx 服务使之生效:
sudo systemctl restart nginx

3.3 配置 HTTPS

要配置 HTTPS,需要先安装证书。可以使用 Let's Encrypt 免费证书,也可以使用其他第三方提供的证书。

下面以使用 Let's Encrypt 证书为例:

  1. 安装 Certbot:
sudo apt-get install certbot python-certbot-nginx
  1. 获取证书:
sudo certbot --nginx -d example.com -d www.example.com

其中,example.com 为自己的域名或者 IP。该命令会自动配置 HTTPS,并且会自动续期证书。

四、示例应用

4.1 反向代理

假设有两个服务,分别运行在 http://localhost:8080http://localhost:8081 上。现在想要使用 Nginx 将这两个服务分别映射到 http://localhost/service1http://localhost/service2

可以按照以下步骤进行:

  1. 修改 Nginx 配置文件:
sudo vim /etc/nginx/sites-available/example.com.conf
  1. 添加以下内容:
location /service1 {
    proxy_pass http://localhost:8080;
}

location /service2 {
    proxy_pass http://localhost:8081;
}
  1. 重启 Nginx 服务使之生效:
sudo systemctl restart nginx

现在就可以通过 http://localhost/service1http://localhost/service2 访问这两个服务了。

4.2 负载均衡

假设有两台服务器,分别运行着 http://server1http://server2 上。现在想要使用 Nginx 来实现负载均衡,将所有请求分发到这两台服务器上。

可以按照以下步骤进行:

  1. 修改 Nginx 配置文件:
sudo vim /etc/nginx/sites-available/example.com.conf
  1. 添加以下内容:
upstream backend {
    server server1;
    server server2;
}

server {
    ...

    location /backend {
        proxy_pass http://backend;
    }
}
  1. 重启 Nginx 服务使之生效:
sudo systemctl restart nginx

现在就可以通过 http://example.com/backend 访问这两台服务器上运行的服务了。请求会被自动分配到这两台服务器上,实现负载均衡。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Nginx 安装与配置规则入门详解 - Python技术站

(0)
上一篇 2023年5月16日
下一篇 2023年5月16日

相关文章

  • Nginx支持websocket的配置详解

    我会提供“Nginx支持websocket的配置详解”的完整攻略,包含以下内容: Nginx支持WebSocket的配置说明 WebSocket代理 示例说明 Nginx支持WebSocket的配置说明 要使Nginx支持WebSocket,需要将HTTP升级为WebSocket协议。因此,在Nginx中使用的proxy_set_header指令必须包括Up…

    Nginx 2023年5月16日
    00
  • php的getallheaders函数在nginx下失效的解决办法

    今天将apache下的php应用部署到了nginx上,却发现报错:找不到getallheaders()这个函数。 很惊奇,这不是php的默认函数么,怎么给我找不到了。但问题就是出现了,只能去找解决方法。 原来此函数是apache_request_headers()函数的别名,看到了吧,带有apache的血统,因此nginx不能用很正常了。 找到原因了,还是需…

    Nginx 2023年4月11日
    00
  • nginx1.16.1平滑升级到1.18

    系统环境:redhat7.6  nginx版本:nginx1.16.1 1、到官网查看最新稳定版的安装包 http://nginx.org/en/download.html 2、查看已经安装nginx的版本以及安装模块 [dip@dip007 nginx]$ /user/local/nginx/sbin/nginx -Vnginx version: ngin…

    Nginx 2023年4月10日
    00
  • nginx暴露目录文件

    location /apk_download { alias /usr/share/nginx/html/; include mime.types; default_type application/octet-stream; autoindex on; autoindex_exact_size off; autoindex_localtime on; ch…

    Nginx 2023年4月13日
    00
  • nginx配置多个站点共用80端口的解决方法

    当一个服务器需要承载多个网站时,我们可以用Nginx实现多站点共用同一IP和端口的方案。其实现的主要步骤如下: 准备工作:确保已经安装了Nginx,并且位于系统环境变量中,可以通过nginx -v命令查看版本信息。 创建站点目录:为每一个网站创建所需的目录,假设有两个站点需要创建: 站点A的目录是:/data/www/siteA/ 站点B的目录是:/data…

    Nginx 2023年5月16日
    00
  • Nginx服务器设置黑名单屏蔽IP

    黑名单能有效防止某个IP恶意攻击或者拒绝特定IP的访问 步骤1 :创建黑名单 在/usr/local/nginx/conf下创建黑名单文件ip.black(叫啥都行)在文件内容写上列入黑名单的IP,格式为deny ip deny 192.168.41.218; deny 192.168.41.217;                             …

    Nginx 2023年4月16日
    00
  • 负载均衡中间件(一)Nginx高性能负载均衡器 linux C++ 通讯架构(一)nginx安装、目录、进程模型

      Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/PO3)代理服务器,并在一个BSD协议下发行,可以在UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows等操作系统中运行。 由俄罗斯的码农lgor Sysover所开发,最初供俄国大型的入口网站及搜寻引擎Rambler使用。…

    Nginx 2023年4月12日
    00
  • (gunicorn | uvicorn)+nginx 部署python-sanic项目

        1、创建app #main.py from sanic import Sanic from sanic.response import json as JsonResponse,text,html from views.user import user_bp app = Sanic(__name__, strict_slashes = False) …

    Nginx 2023年4月13日
    00
合作推广
合作推广
分享本页
返回顶部