关于 nginx 前端知道这些就够了

算法踏风鹤
• 阅读 6097

我备案了个域名,买了一个阿里云服务器,想要搭建几个自己的网站,难免要接触 nginx

那么我用 nginx 来干嘛呢:

  1. 静态资源反向代理
  2. 将域名泛解析到服务器之后,通过 nginx 来给不同的二级域名分配服务器上的程序。

1、安装 nginx

1.1、centos7

yum install -y nginx

1.2、windows

到官网下载,安装包,解压即可使用:官网

1.3、centos7 中 nginx 常用命令

# 开机启动
systemctl enable nginx.service

# 启动
systemctl start nginx.service

# 使用某个配置文件启动(要先关闭 ngxin,不然会报错的)
nginx -c /etc/nginx/nginx.conf

# 关闭(如果这样关闭不了的话,就把 nginx 进程给杀掉)
nginx -s stop

# 查看 nginx 的进程 id
ps -ef | grep nginx

# 杀死进程(比如杀死进程1234)
kill -9 1234

nginx 的默认配置文件是:/etc/nginx/nginx.conf

这个配置文件会自动读取:/etc/nginx/conf.d/ 文件夹下的所有 .conf 文件。

假如你写了个网站,需要用到 nginx ,那么你就把配置文件丢到 /etc/nginx/conf.d/ 文件夹下,然后重启 nginx 就行了:

nginx -s stop
nginx -c /etc/nginx/nginx.conf

2、nginx 配置(反向代理设置 & 二级域名)

2.1 配置文件

假设我们有两个网站:
www.example.com
blog.example.com

它们分别有两个 ngxin 配置文件放在:
/home/www.example.com/www.example.com.nginx.conf
/home/blog.example.com/blog.example.com.nginx.conf

服务器对外开放 80 端口,两个网站对应的程序端口分别为:

www.example.com 程序 8000
www.example.com 静态资源 8080

blog.example.com 程序 8001
blog.example.com 静态资源 8081

那么他们的配置内容分别为:

# /home/www.example.com/www.example.com.nginx.conf
server {
    # 服务器对外只监听 80 端口
    listen 80;
    # 当 80 端口监听到了来自 www.example.com 的请求
    server_name www.example.com;
    # 那么把这个请求转到 http://localhost:8080
    location / {
        proxy_pass http://localhost:8080;
    }
}
server {
    # 监听到 8080 端口有人发起请求(其实就是上面转过来的↑,用户不能直接访问 8080 端口的)
    listen 8080;
    # 只允许本机访问
    server_name localhost;
    # 程序根目录
    root /home/www.example.com;
    # 默认的网页文件
    index index.html index.htm;
    # 匹配所有 uri (如果 url 为:http://www.example.com/hehe,那么 uri 为:/hehe)
    location / {
        # 我们的程序实际上是在 8000 端口上
        proxy_pass http://localhost:8000$request_uri;

        # 下面这一大堆,欲知详情自己查
        proxy_redirect     off;
        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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
    # 匹配首页
    location = / {
        proxy_pass http://localhost:8000;
    }
    # 匹配 /**.** 结尾的 uri,并且不是 /**.html、/**.htm
    location ~* ^.+\/[^\/]+(?=\.)([^\/](?!(html|htm)))+$ {
      # 匹配到的都定位到静态资源目录里
      root /home/www.example.com/static;
      etag         on;
      expires      max;
    }
}
# /home/blog.example.com/blog.example.com.nginx.conf
server {
    # 服务器对外只监听 80 端口
    listen 80;
    # 当 80 端口监听到了来自 blog.example.com 的请求
    server_name blog.example.com;
    # 那么把这个请求转到 http://localhost:8081
    location / {
        proxy_pass http://localhost:8081;
    }
}
server {
    # 监听到 8081 端口有人发起请求(其实就是上面转过来的↑,用户不能直接访问 8081 端口的)
    listen 8081;
    # 只允许本机访问
    server_name localhost;
    # 程序根目录
    root /home/blog.example.com;
    # 默认的网页文件
    index index.html index.htm;
    # 匹配所有 uri (如果 url 为:http://blog.example.com/hehe,那么 uri 为:/hehe)
    location / {
        # 我们的程序实际上是在 8001 端口上
        proxy_pass http://localhost:8001$request_uri;

        # 下面这一大堆,欲知详情自己查
        proxy_redirect     off;
        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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
    # 匹配首页
    location = / {
        proxy_pass http://localhost:8001;
    }
    # 匹配 /**.** 结尾的 uri,并且不是 /**.html、/**.htm
    location ~* ^.+\/[^\/]+(?=\.)([^\/](?!(html|htm)))+$ {
      # 匹配到的都定位到静态资源目录里
      root /home/blog.example.com/static;
      etag         on;
      expires      max;
    }
}

注意看两个配置的区别。

2.2 创建软链接

假如我们每个网站程序放在一个文件夹里,该程序的 nginx 配置文件也应该放在这个文件夹里才方便管理。但前面提到,我们需要把配置文件丢到 /etc/nginx/conf.d/ 文件夹下,怎样才能使这个配置文件既在程序文件夹下,又在 /etc/nginx/conf.d/文件夹下呢?

假如我们在程序文件夹下有一个 ngxin 配置文件:/home/www.example.com/www.example.com.nginx.conf

我们需要给这个文件创建一个软链接到 /etc/nginx/conf.d/ 下:

ln -s /home/example/www.example.com.nginx.conf /etc/nginx/conf.d/www.example.com.nginx.conf

这样操作之后,当我们改程序文件夹下的配置文件,/etc/nginx/conf.d/ 下与之对应的配置文件也会被修改,修改后重启 nginx 就能够使新的 ngxin 配置生效了。

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
美凌格栋栋酱 美凌格栋栋酱
10个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
待兔 待兔
1年前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
芝士年糕 芝士年糕
3年前
nginx 启动、停止、关闭
使用3A服务器搭建的centos系统安装nginx教程在我的往期博客中1,nginx指定配置文件/usr/local/nginx/sbin/nginxc/usr/local/nginx/conf/nginx.conf1c参数指定了要加载的nginx配置文件路径1,从容停止Nginx:killQUIT主进程号2,快速停止Nginx:kil
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
4年前
Nginx配置https
一、开启nginx的ssl模块1.未安装过nginx,编译安装配置参数如下:./configure\prefix/usr/local/nginx\withpcre\withhttp\_ssl\_modulessl模块\withhttp\_stub\_status\_module\wit
Wesley13 Wesley13
4年前
4. Nginx模块
Nginx官方模块1.ngx\_http\_stub\_status\_modulehttp://nginx.org/en/docs/http/ngx\_http\_stub\_status\_module.html。(https://www.oschina.net/action/GoToLink?urlhttp%3A%2
Stella981 Stella981
4年前
Python之time模块的时间戳、时间字符串格式化与转换
Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y
Stella981 Stella981
4年前
Nginx
!(https://imagestatic.segmentfault.com/255/117/25511790966008dc5b00fd8)Nginx进程模型分析在介绍Nginx的进程模型之前我们先来给大家解释下一些常见的名词,这能辅助我们更好的了解Nginx的进程模型。作为Web服务器,设计的初衷就是为了能够处理更多的客户端的请
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这