首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ubuntu 20.04中安装nginx 流程(通过install 的方式)

ubuntu 20.04中安装nginx 流程(通过install 的方式)

作者头像
fruge365
发布2025-12-15 11:12:12
发布2025-12-15 11:12:12
2370
举报

ubuntu20.04 安装 nginx 操作流程

1.更新软件包列表

sudo apt update

2.安装最新版本的nginx

sudo apt install nginx

或者安装指定版本的nginx

  • 查看可安装的版本 sudo apt-cache show nginx
  • 安装指定版本 sudo apt install nginx=<version>

3.安装完后查看安装的版本

nginx -v

4.路径位置

log: /var/log/nginx

html:/var/www/html

conf:/etc/nginx

5.查看nginx状态

systemctl status nginx

6.停止nginx

sudo systemctl stop nginx

7.启动nginx

sudo systemctl start nginx

8.重启nginx

sudo systemctl restart nginx

9.实例配置文件

代码语言:javascript
复制
user www-data;
worker_processes auto;

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;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;

    include /etc/nginx/conf.d/*.conf;

    upstream backend {
        server 127.0.0.1:8080;
    }

    server {
        listen 80;
        server_name example.com www.example.com;
        root /usr/share/nginx/example;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location /api/ {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        location ~ /\.ht {
            deny all;
        }
    }

    server {
        listen 80;
        server_name test.com www.test.com;
        root /usr/share/nginx/test;

        location / {
            try_files $uri $uri/ =404;
        }

        location /secure/ {
            auth_basic "Restricted";
            auth_basic_user_file /etc/nginx/.htpasswd;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-08-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ubuntu20.04 安装 nginx 操作流程
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档