nginx 配置文件示例 (精简)

本文主要给出各场景下 nginx 的配置文件示例, 虽然精简, 可是都可以运行.node

前提

经过 nginx -t 查看本机 nginx 配置文件是哪一个, 将下面示例进行替换, 而后 nginx -s reload 重启 nginx 便可.nginx

note: 覆盖原有的 nginx 配置文件以前, 能够对原来的配置文件进行备份 cp nginx.conf nginx.conf.bak

配置静态服务器

events {
    worker_connections  1024;
}

http {
    server {
      listen 8080;

      location / {
        root /data/www; # 替换为静态文件目录
      }
    }
}

多端口静态资源服务

events {
    worker_connections  1024;
}

http {
    server {
      listen 8081;

      location / {
        root /data/www2; # 端口 1 静态文件目录
      }
    }

    server {
      listen 8080;

      location / {
        root /data/www; # 端口 2 静态文件目录
      }
    }
}

配置代理

events {
    worker_connections  1024;
}

http {
    server {
      listen 8080;

      location / {
        root /data/www;
      }
    }

    server {
      listen 8081; # 代理端口

      location / {
        proxy_pass http://localhost:8080; # 代理到什么路径
      }

    }
}

配置 HTTPS

若是是自测 https, 能够生成自签名证书bash

cd ~/cert
openssl genrsa 2048 > host.key
chmod 400 host.key
openssl req -new -x509 -nodes -sha256 -days 365 -key host.key -out host.cert

配置文件为服务器

events {
    worker_connections  1024;
}

http {
    server {
      listen 80;
      listen 443 ssl;
      ssl_certificate     /root/cert/host.cert;
      ssl_certificate_key /root/cert/host.key;

      location / {
        root /data/www;
      }

    }
}

待补充...

(若有错误或不一样的看法, 望不吝指出, 愿共同进步!)代理

相关文章
相关标签/搜索