#user nobody;
#==The number of work processes is generally set to the number of CPU cores
worker_processes1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#==Maximum number of connections, generally set to cpu*2048
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#==Client link timeout time
keepalive_timeout65;
#gzip on;#When configuring multiple server nodes, the cache size of the default server names is not enough, and you need to manually set it to be larger.
server_names_hash_bucket_size512;#server means that a virtual host can be understood as a site, and multiple server nodes can be configured to build multiple sites.
#Each request comes in to determine which server to use is determined by server_name
server{#Site listening port
Listen8800;#Site access domain name
server_name localhost;#Encoding format, avoid garbled url parameters
charset utf-8;
#access_log logs/host.access.log main;#location is used to match multiple domain namesURIAccess rules
#For example, how to jump dynamic resources, how to jump static resources, etc.
#location followed by/Represents the matching rules
location/ {#The site root directory can be a relative path or an absolute path
root html;#Default Homepage
index index index.html index.htm;#Forwarding backend site addresses, generally used for soft loads and polling backend servers
#proxy_pass http://10.11.12.237:8080;#Reject request, return403, generally used for certain directories to prohibit access
#deny all;#Allow requests
#allow all;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';#Redefine or add request headers sent to the backend server
#Add the client request hostname to the request header
proxy_set_header Host $host;#Add client to the request headerIP
proxy_set_header X-Real-IP $remote_addr;#Add the $remote_addr variable value to the client"X-Forwarded-For" after the request header, separated by commas. If the client request does not carry "X-Forwarded-For” request header, the value of $proxy_add_x_forwarded_for variable will be the same as the $remote_addr variable
proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;#Add client's cookies to the request header
proxy_set_header Cookie $http_cookie;#The proxy server's primary domain name and port number will be replaced. If the port is80, no addition.
proxy_redirect off;#The browser has many restrictions on cookies, and it cannot be written if the Domain part of the cookie does not match the Domain of the current page.
#So if requestedADomain name, server proxy_pass toBDomain name, thenBServer output Domian=BCookies,
#The front-end page is still stuckAOn the domain name, the browser cannot write cookies.
#Not only domain names, browsers also have restrictions on Path. We often proxy_pass to a certain Path on the target server.
#Don't expose this Path to the browser. At this time, if the target server's cookie is written to the Path, the problem of the cookie being unable to be written will also occur.
#Set-Cookie" response to the replacement text of the domain attribute in the header, whose value can be a string, a regular expression pattern, or a referenced variable
#Forwarding the backend server If you need cookies, you need to convert the cookie domain as well. Otherwise, the front-end domain name and the back-end domain name will not be consistent with the cookie, and the cookie will not be able to be stored or retrieved.
#Configuration rules: proxy_cookie_domainserverDomain(Backend server domain) nginxDomain(nginx server domain)
proxy_cookie_domain localhost .testcaigou800.com;#Cancel all proxy_cookie_domain directives at the current configuration level
#proxy_cookie_domain off;#Timeout time for establishing a connection with the backend server. It is generally impossible to be greater than75Second;
proxy_connect_timeout30;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}#When you need to listen to multiple domain names on the same port, use the following configuration. The same port has different domain names, and server_name can also be configured using regular
#But be aware that there are too many servers and you need to manually expand the size of the server_names_hash_bucket_size cache area
server{
listen 80;
server_name www.abc.com;
charset utf-8;
location / {
proxy_pass http://localhost:10001;
}
}
server {
listen 80;
server_name aaa.abc.com;
charset utf-8;
location / {
proxy_pass http://localhost:20002;
}
}
}