1.nginx.conf 配置文件引入子配置文件 include vhost-server/*.conf; 关键在 server_name 不同域名执行不同的根
user root; #user nobody; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { client_max_body_size 20m; include mime.types; default_type application/octet-stream; underscores_in_headers on; #add_header X-Frame-Options SAMEORIGIN; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/dev_access.log; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; include vhost-server/*.conf; }
2. vhost-server 下子配置 a.conf b.conf
a.conf
server { listen 80; listen 443 ssl; server_name test.com; #ssl on; ssl_certificate cert/test/test.pem; ssl_certificate_key cert/test/test.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; error_log logs/sitxqy_error.log; access_log logs/sitxqy_access.log; autoindex on; # 显示目录 autoindex_exact_size on; # 显示文件大小 autoindex_localtime on; # 显示文件时间 root html/test/; index index.html; client_max_body_size 20m; location /api/ { proxy_pass http://localhost:88/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # location ~ .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|less|map|eot|svg|ttf|woff|woff2|cab|scss|otf|exe|pdf|doc|xlsx)$ { # root html/xqc-h5/cloudSign; # index index.html; # } location / { try_files $uri $uri/ /index.html; } location /a { try_files $uri $uri/ /a/index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; index index.html; } }
b.conf
server { listen 80; listen 443 ssl; server_name testa.com; #ssl on; ssl_certificate cert/testa.pem; ssl_certificate_key cert/testa.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; error_log logs/sitxqy_error.log; access_log logs/sitxqy_access.log; autoindex on; # 显示目录 autoindex_exact_size on; # 显示文件大小 autoindex_localtime on; # 显示文件时间 root html/cloudSignOpen; index index.html; client_max_body_size 20m; location /api/ { proxy_pass http://localhost:88/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location / { try_files $uri $uri/ /index.html; } location /a { try_files $uri $uri/ /a/index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; index index.html; } }