主文件配置:https://www.cnblogs.com/yjlch1016/p/9313463.html
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
#動態模塊加載
include /usr/share/nginx/modules/*.conf;
#連接數
events {
worker_connections 1024;
}
http {
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;
#選項能提高 web server性能
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 20m;
#Nginx 會根據mime type定義的對應關係來告訴瀏覽器如何處理服務器傳給瀏覽器的這個文件,是打開還是下載;
#如果Web程序沒設置,Nginx也沒對應文件的擴展名,就用Nginx 裏默認的 default_type定義的處理方式。
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#加載配合模塊
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
配置文件數據:
在config.d文件夾寫可以配置獨立的模塊數據
比如針對一個服務的可以進行配置
upstream proxy-www-test {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name web.test.com;
access_log /var/log/nginx/test/www.acess.log main;
error_log /var/log/nginx/test/www.error.log;
root html;
index index.html index.htm index.php;
location / {
proxy_pass http://proxy-www-test;
#Proxy Settings
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name www.test.com;
access_log /var/log/nginx/test/www.acess.log main;
error_log /var/log/nginx/test/www.error.log;
root html;
index index.html index.htm index.php;
location / {
proxy_pass http://proxy-www-test;
#Proxy Settings
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name test.com;
access_log /var/log/nginx/test/www.acess.log main;
error_log /var/log/nginx/test/www.error.log;
root html;
index index.html index.htm index.php;
location / {
proxy_pass http://proxy-www-test;
#Proxy Settings
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
加載這個配置文件後 啓動nginx
這樣 通過域名 www.test.com 、web.test.com、test.com都可以訪問到upstream的服務。