首页 > 系统相关 >【LalaLuna笔记】Nginx基础配置(1)

【LalaLuna笔记】Nginx基础配置(1)

时间:2022-08-25 18:27:10浏览次数:93  
标签:index www LalaLuna nginx 笔记 Nginx html conf log

nginx启动命令是什么

第一种方法:进入sbin目录下执行以下命令:

启动nginx的命令为
/usr/local/nginx/sbin/nginx

停止nginx的命令为
/usr/local/nginx/sbin/nginx -s stop

重启nginx的命令为
/usr/local/nginx/sbin/nginx -s reload

第二种方法:配置systemctl之后的启动方式

systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx

准备工作,从备份生成nginx.cof

1.之前的操作修改了 nginx.conf,重新生成一个 nginx.conf

mv nginx.conf nginx_1.conf
# 此时logs文件夹里就没有 nginx.conf了

cp nginx.conf.default nginx.conf
# 使用备份文件 nginx.conf.default

vim nginx.conf

2.目录结构

/home/www
├── conf.d  
├── myweb 
    ├── 404.html
    ├── server1
    │   ├── location1 
    │   │   └── index_sr1_location1.html
    │   ├── location2
    │   │   └── index_sr1_location2.html
    │   └── logs
    └── server2
        ├── location1
        │   └── index_sr2_location1.html
        ├── location2
        │   └── index_sr2_location2.html
        └── logs

3.修改 nginx.conf

user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认就是on,所以注释掉了

events {
    accept_mutex on;
    multi_accept on;
    worker_connections 1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout    65;
    log_format server1 '==========> server1 access log format';
    log_format server2 '==========> server2 access log format';
    include /home/www/conf.d/*.conf;
    
    server {
        listen 80;
        server_name localhost;
        access_log logs/server.log;
        location / {
            root /home/www/html;
            index index.html index.htm;
        }
        location /get_text {
            access_log logs/text.log;
            default_type text/html;
            return 200 "<h1>This is text</h1>";
        }
        location /get_json {
            default_type application/json;
            return 200 '{"name": "Tom", "age": 18}';
        }
    }
}

4.分别配置server1和server2的conf

vim /home/www/conf.d/server1.conf
server {
    listen 8081;
    server_name localhost;
    access_log  /home/www/myweb/server1/logs/access.log server1;
    location /server1/location1 {
        root /home/www/myweb;
        index index_sr1_location1.html;
    }
    location /server1/location2 {
        root /home/www/myweb;
        index index_sr1_location2.html;
    }
    
    error_page 404 /404.html
    location = /404.html {
        root /home/www/myweb;
        index 404.html;
    }
}
vim /home/www/conf.d/server2.conf
server {
    listen 8082;
    server_name localhost;
    access_log  /home/www/myweb/server2/logs/access.log server2;
    location /server2/location1 {
        root /home/www/myweb;
        index index_sr2_location1.html;
    }
    location /server2/location1 {
        root /home/www/myweb;
        index index_sr2_location2.html;
    }
    
    error_page 404 /404.html
    location = /404.html {
        root /home/www/myweb;
        index 404.html;
    }
}

Nginx配置成系统服务

(1)在 /usr/lib/systemd/system 目录下添加nginx.service

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
wantedBy=default.target

(2)添加完成后如果有权限问题

chmod 755 /usr/lib/systemd/system/nginx.service

(3)使用

启动:systemctl start nginx
停止:systemctl stop nginx
重启:systemctl restart nginx
重新加载配置文件:systemctl reload nginx
查看nginx状态:systemctl status nginx
开机启动:systemctl enable nginx # Linux服务器重启后,自动运行nginx

Nginx命令配置到系统命令

(1)修改/etc/profile文件

vim /etc/profile
# 最后一行加上
export PATH=$PATH:/usr/local/nginx/sbin

(2)使之立即生效

source /etc/profile

(3)执行nginx命令

nginx -V

标签:index,www,LalaLuna,nginx,笔记,Nginx,html,conf,log
From: https://www.cnblogs.com/lalaluna/p/16625268.html

相关文章

  • 成员变量笔记
    1.方法使用1)方法写好后,如果不调用,不会输出2)先创建对象,然后调用方法即可2.publicvoidspeak(){}1)public表示方法是公开的2)void表示方法没有返回值,若为int,表示方法执行后,返回......
  • ABP-VNEXT 学习笔记(四)自动API 控制器
    官方文档地址:https://docs.abp.io/en/abp/latest/API/Auto-API-Controllers 详细的请阅读官方文档,这边侧重简化说明怎么应用和一些注意要点。自动API,即代码端只需要定......
  • 差分学习笔记
    文章引自洛谷博客。欢迎来到蒟蒻$\texttt{Orange}$的差分学习笔记!蒟蒻$\texttt{Orange}$的第一篇笔记!(雾)接下来是差分学习时刻!!注:Orange的$\LaTeX$很差,装饰的很......
  • docker 搭建 nginxconfig.io 文档
    docker镜像仓库https://hub.docker.com/r/devopstestlab/nginxconfig.io获取镜像docekrpulldevopstestlab/nginxconfig.io运行镜像dockerrun-it......
  • vue3 学习笔记
    watchletsum=ref('0');letperson=reactive({sex:‘女’,age:18,}) watch(sum,(oldVal,newVal)=>{console.log(oldVal,newVal);})/**监视reactive所定义的一......
  • iptables学习笔记
    原文链接:朱双印的iptables博客目录第一章基本概念第二章常用命令第三章iptables匹配条件第四章iptables扩展匹配条件第五章iptables的黑白名单机制第六章iptables自......
  • c#通过表达式树优雅的实现分组取TopN笔记
    需要引入nuget包来实现ef.functions调用row_numberThinktecture.EntityFrameworkCore.SqlServer调用方式://顺排context.Table.GroupBySortTop(1,x=>x.partitionP......
  • 论文阅读笔记-Gen-LaneNet: A Generalized and Scalable Approach for 3D Lane Detect
    Gen-LaneNet:AGeneralizedandScalableApproachfor3DLaneDetectionGen-LaneNet:一种通用且可扩展的3D车道检测方法Abstract我们提出了一种通用且可扩展的方法,......
  • 网络安全笔记(Seventeen Days)
    SeventeenDays交换机的工作原理一、以太网(局域网)的MAC1、交换机的概念交换机它是属于数据链路层的设备,数据链路层所传输的是数据帧,所封装的是MAC头部(主要有源MAC地......
  • 在本地使用Markdown来写作,自动同步文章笔记到印象笔记(马克飞象替代方案)
    在本地使用 MarkdownPad 、Mark 、Typora 、VSCode 任意你喜欢的 Markdown 编辑器进行文档编写,本工具将自动同步文档到印象笔记。可以当做是马克飞象的替代方案。......