0
点赞
收藏
分享

微信扫一扫

ubuntu 20.04中安装nginx 流程

Mhhao 2024-08-03 阅读 17

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.实例配置文件

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;
        }
    }
}
举报

相关推荐

0 条评论