• Home
  • 归档
文章检索 作品与友邻 关于我
暴君乔博客
Home 归档

nginx从入门到无所不能

发布于: 2021-05-18
更新于: 2023-07-09
文章目录
  1. nginx
    1. 代理
    2. 配置项
    3. docker-compose 配置项

nginx

Nginx 是一款轻量级的 Web 服务器,反向代理服务器及电子邮件(IMAP/POP3)代理服务器

代理

正向代理

反向代理

配置项

# HTTP服务器
server {
  # 监听80端口,用于HTTP协议
  listen 80;
  # 如果需要ssl端口的话,可以参考下面的另外一套配置

  # 首页指向
  index index.php index.html;

  # 定义适用localhost进行访问
  server_name localhost;

  # 日志文件记录地址
  error_log  /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;

  # 指向webapp的目录地址
  root /api/;
  # 首页指向
  index index.php index.html index.htm default.php default.htm default.html;

  #添加响应头
  add_header Content-Security-Policy "upgrade-insecure-requests'";

  # 将所有的请求都rewrite成index.php的形式
  # 反向代理的地址
  location / {
    try_files $uri $uri/ /index.php$is_args$args;
    # try_files $uri $uri/ @api;
  }

  # 弃用版本,等后面学会了再说吧
  # location @api {
  #   rewrite /api/(.*)$ /api/index.php?/$1 last;
  # }

  # 所有重写的url 以及以.php结尾的URI,都由location ~ .php{}块进行管理
  location ~ \.php(/|$) {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

  # Nginx状态的地址
  location /NginxStatus {
      stub_status on;
      access_log    on;
      auth_basic "NginxStatus";
      auth_basic_user_file conf/htpasswd;
  }
}

ssl 配置项

支持 https,需要申请证书,证书腾讯云等网上一堆免费的,不再赘述

server {
  # 监听 443 端口,用于HTTPS协议
  listen 443 ssl;

  #填写绑定证书的域名
  #server_name cloud.tencent.com;
  server_name tencent.tyrantqiao.com;
  #证书文件名称
  #ssl on;
  ssl_certificate cert/1_tencent.tyrantqiao.com_bundle.crt;
  #私钥文件名称
  ssl_certificate_key cert/2_tencent.tyrantqiao.com.key;
  ssl_session_timeout 5m;
  #请按照以下协议配置
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

  #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  ssl_prefer_server_ciphers on;

  # 首页指向
  index index.php index.html;

  server_name localhost;

  # 日志文件记录地址
  gzip on;
  error_log  /var/log/nginx/error.log ;
  access_log /var/log/nginx/access.log ;

  # 指向webapp的目录地址
  root /api;
  # 首页指向
  index index.php index.html index.htm default.php default.htm default.html;

  #添加响应头 https
  add_header Content-Security-Policy "upgrade-insecure-requests'";
  # 允许跨域
  add_header Access-Control-Allow-Origin *;
  #add_header Access-Control-Allow-Headers *;
  #add_header Access-Control-Allow-Methods GET,POST,OPTIONS,PUT,DELETE;

  # 将所有的请求都rewrite成index.php的形式
  # 反向代理的地址
  location / {
    # try_files 会尝试列出的文件并设置内部文件指向
    # 比如说 localhost/test  $uri=test 如果存在/$root/test的文件,那么就发送给客户
    # 如果上面找不到,那就找有没有名为/$root/test的目录,如果还没有则尝试最后一项
    # 尝试发起/index.php的内部请求,而这个就会被下面的php{}块进行管理
    # /api/get?id=1
    # $is_args 是否携带了queryString,携带为'?' 没有就为''
    # $query_string=$args = 'id=1'
    # last url重写后,直接发起新请求,进入server块,重新尝试location匹配
    # break 直接使用当前资源,完成请求,rul不变
    # redirect 302临时重定向,地址栏显示重定向的,爬虫不会更新url
    # permanent 301永久重定向,地址栏显示重定向的,爬虫更新url
    try_files $uri $uri/ /index.php$is_args$query_string last;
    # try_files $uri $uri/ @api;
  }

  # 弃用版本,等后面学会了再说吧
  # location @api {
  #   rewrite /api/(.*)$ /api/index.php?/$1 last;
  # }

  # 所有重写的url 以及以.php结尾的URI,都由location ~ .php{}块进行管理
  location ~ \.php$ {
    try_files $uri =404;
    # fastcgi_split_path_info作用
    # 将$1赋值给 $fastcgi_script_name
    # 将$2赋值给 $fastcgi_path_info
    fastcgi_split_path_info ^(.+?\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

  # Nginx状态的地址
  location /NginxStatus {
      stub_status on;
      access_log    on;
      auth_basic "NginxStatus";
      auth_basic_user_file conf/htpasswd;
  }
}

docker-compose 配置项

version: "3"
services:
  nginx:
     image: nginx:latest
     restart: always
     container_name: php-nginx
     volumes:
      - ./api:/api
      - ./nginx/cert:/etc/nginx/cert
      - ./nginx/conf.d:/etc/nginx/conf.d/
      - ./nginx/log:/var/log/nginx
     ports:
      - "xxx:443"
     environment:
       TZ: 'Asia/Shanghai'
     depends_on:
      - php
     networks:
      docker_composer_net:
       aliases:
         - nginx
  php:
    image: php:7.2-fpm
    restart: always
    ports:
      - "9000:9000"
    container_name: php
    volumes:
      - ./api:/api
    networks:
     docker_composer_net:
        aliases:
          - php
networks:
  docker_composer_net:
nginx从入门到无所不能
/archives/d0ccd42c/
作者
tyrantqiao
发布于
2021-05-18
更新于
2023-07-09
许可协议
CC BY-NC-SA 4.0
赏

蟹蟹大佬的打赏,大家一起进步

支付宝
微信
  • nginx

扫一扫,分享到微信

微信分享二维码
如何设计消息中间件
再学Java五百年
© 2026 tyrantqiao 本站总访问量次 本站访客数人次 载入天数...载入时分秒...
  • 文章检索
  • 作品与友邻
  • 关于我

展开标签

  • 随笔
  • 旅行
  • Java
  • 面经
  • 源码
  • 架构
  • Hadoop
  • 计算机网络
  • 技术
  • IDEA
  • maven
  • JDK
  • Linux
  • MySQL
  • 性能
  • 数据库
  • Redis
  • MQ
  • Spring
  • SquirrelFin
  • 记账
  • Android
  • 个人财务
  • TIDB
  • unity
  • chatgpt
  • 前端
  • Vue
  • Git
  • shadowsocks
  • Hexo
  • 问题排查
  • 业务
  • JVM
  • 算法
  • nginx
  • ssh
  • Python
  • 爬虫
  • test
  • vim
  • 影视剧
  • 中间件
  • 事务
  • 表达
  • 运动
  • mybatis
  • 域名
  • AI
  • 产品
  • 家装
  • 土巴兔
  • github
  • 软件
  • 个人项目
  • Leaves
  • 幽默
  • 穿搭
  • 画画
  • 缓存
  • 摄影
  • 设计模式
  • c
  • 课程设计
  • 调酒
  • 数学

Collection

作品与友邻

我做的小工具、朋友的站点和一些长期值得回访的灵感来源。

  • 土 土巴兔 AI 导购交互演示 可点击体验用户导购、平台运营、装修公司、材料商及 AI 话术知识库的多端增长流程。 /demos/tubatu-ai-guide/
  • L Leaves 行程记录 我做的行程记录页面小工具,用来沉淀旅行路线、日程安排和途中的灵感。 https://leaves.tyrantqiao.com/
  • 本 本网站主题yilia设计者的主页 http://litten.me/
tyrantqiao的头像

tyrantqiao

github rss qq mail facebook linkedin

About

关于我

代码、海浪和持续折腾,都是生活的一部分。

<div class="aboutme-card"> <p class="aboutme-eyebrow">tyrantqiao</p> <h3>Java 全栈开发</h3> <p>常年和 Java、后端服务、前端工程、数据与部署打交道,喜欢把复杂问题拆开、写清楚,也把踩坑过程整理成文章留给后来的人。</p> <div class="aboutme-tags"> <span>Java</span> <span>Full Stack</span> <span>Backend</span> <span>Frontend</span> <span>Extreme Sports</span> </div> <p>工作之外偏爱冲浪等极限运动,享受在浪上找平衡,也享受在代码里找边界。</p> <div class="aboutme-contact"> <a href="mailto:[email protected]">Gmail</a> <a href="mailto:[email protected]">QQ Mail</a> </div> </div>