让多个nginx配置文件相互独立有助于后期维护和开发
1 多个server配置文件
# nginx.conf文件
http { include /etc/nginx/mime.types; default_type application/octet-stream; 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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
在/etc/nginx/conf.d/目录下添加多个server文件
# server_test1.conf
server { listen 80; listen [::]:80; server_name test1.com; location /api/ { # 设置代理服务器 proxy_pass http://172.18.2.100:5000/; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Nginx-Proxy true; } }
# server_test2.conf
server { listen 80; listen [::]:80; server_name test2.com; location /api/ { # 设置代理服务器 proxy_pass http://172.18.2.101:5000/; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Nginx-Proxy true; } }
2 多个location配置文件
在server配置文件的基础上配置配置多个location,例如:在server_test1.conf上配置location
# server_test1.conf
server { listen 80; listen [::]:80; server_name test1.com; location /api/ { # 设置代理服务器 proxy_pass http://172.18.2.100:5000/; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Nginx-Proxy true; } include /etc/nginx/conf.d/location/*.conf; }
在/etc/nginx/conf.d/location/目录下添加多个location文件
# location_test1.conf
location /location/test1/ { # 设置代理服务器 proxy_pass http://127.0.0.1/test1.index; }
# location_test2.conf
location /location/test2/ { # 设置代理服务器 proxy_pass http://127.0.0.1/test2.index; }
3 配置无授权证书的https网站
使用重定向,不需要配置授权证书,适合测试用。如果,配置https证书比较繁琐
location /test { rewrite "^/test/(.*)$" https://127.0.0.1:5443/openmeetings/$1 break; }