nginx 配置多个站点
1. 默认server增加location
# 第一个站点配置
location / {
root /data/apps/a;
index index.html index.htm;
}
# 部署的后端访问地址和端口号
location /a-api/ {
proxy_pass http://127.0.0.1:8080/;
}
# 第二个站点配置
location /b {
alias /data/apps/b;
index index.html index.htm;
}
location /b-api/ {
proxy_pass http://127.0.0.1:8081/;
}也可以全部用alias,比root清晰,root会在末尾加上匹配路径,容易疏忽
location /a {
alias /projects/a/;
#指定主页
index index.html;
#自动跳转
autoindex on;
}
location /b {
alias /projects/b/;
#指定主页
index index.html;
#自动跳转
autoindex on;
}2. 增加新的server
在默认的server下面增加新的server即可,可以直接加在下面,也可以新建一个vhost文件,include这个文件
server的重点,是root设置的目录不同
server {
listen 80;
server_name www.demo1.com demo1.com;
location / {
root /usr/local/nginx/web/demo1;
index demo1.htm demo1.html;
}
}
server {
listen 80;
server_name www.demo2.com demo2.com;
location / {
root /usr/local/nginx/web/demo2;
index demo2.htm demo2.html;
}
}mkdir /etc/nginx/vhost # 在默认server下面,加相对路径引入 include vhost/*.conf # 或者全路径 include /usr/local/nginx/vhost/*.conf
nginx 代理多个个站点
1. proxy_pass代理
server {
location /a/ {
proxy_pass http://www.baidu.com/;
}
location /b/ {
proxy_pass http://www.163.com/;
}
}注意:location和proxy_pass后面的/必须保留
2. rewrite代理
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
try_files $uri /index.html;
rewrite ^/a/(.*)$ https://a.com/$1 break;
rewrite ^/b/(.*)$ https://b.com/app/$1 break;
}
}宝塔单域名子目录站点
可直接在 “伪静态” 内填写;假设域名为 “www.example.com”。
# 注意 alias 和 root 的区别
## 访问:`http://www.example.com/example_1/index.html`
## 实际:`/www/wwwroot/example_site/example_1/index.html`
## 注意:不要漏掉末尾的 `/`,以免产生目录穿越漏洞
location ^~ /example_1/ {
root /www/wwwroot/example_site;
# 可选,默认与“server”中指定的一致
index index.php index.html;
# 启用 PHP,仅在使用“root”时可用
include /www/server/nginx/conf/enable-php-74.conf;
}
## 访问:`http://www.example.com/example_2/index.html`
## 实际:`/www/wwwroot/example_site/example_2/index.html`
location ^~ /example_2/ {
alias /www/wwwroot/example_site/example_2/;
index index.php index.html;
}enable-php-74.conf 文件中的内容(该文件自带,不需要创建或修改):
location ~ [^/]\.php(/|$) { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi-74.sock; fastcgi_index index.php; include fastcgi.conf; include pathinfo.conf; }
负载均衡
upstream example_servers { server 192.168.1.101:8080 weight=30; server 192.168.1.102:8080 weight=20; server 192.168.1.103:8080 weight=10; # weight 可选,表示权重,值越大则被分配到的几率越大 } server { listen 80; server_name www.example.com; location / { proxy_pass http://example_servers; } }
禁止访问
禁止访问部分扩展名文件
location ~ \.(conf|ini|php)$ { deny all; }
禁止访问目录
location ^~ /example/ { deny all; }
添加跨域响应头
location / { # add_header 'Access-Control-Allow-Origin' '$http_origin'; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, POST'; add_header 'Access-Control-Allow-Headers' 'Cache-Control, Content-Type, User-Agent, X-Requested-With'; add_header 'Access-Control-Allow-Credentials' 'true'; if ($request_method = 'OPTIONS') { return 204; } }
一个 server 配置多个域名
server_name 中以空格分隔多个域名:
server { listen 80; server_name a.frost-zx.top b.frost-zx.top; ... }
一个 server 配置多个类型的 SSL 证书
参考:Module ngx_http_ssl_module
注意:只有 OpenSSL 1.0.2 及以上版本支持
将 ssl_certificate 和 ssl_certificate_key,复制多一份,然后把路径修改为其他类型证书的路径:
server { listen 443 ssl; server_name example.com; ssl_certificate example.com.rsa.crt; ssl_certificate_key example.com.rsa.key; ssl_certificate example.com.ecdsa.crt; ssl_certificate_key example.com.ecdsa.key; ... }
域名重定向
server { ... if ($host ~ '^www.example.com') { return 301 https://test.example.com/; } ... }
原文转载自:https://www.cnblogs.com/mengff/p/16794156.html
原文转载自:https://blog.frost-zx.top/content/nginx-config-snippet.html
返回顶部
