參考原文地址:https://www.cnblogs.com/hailang8/p/8664413.html
參考原文地址:https://www.cnblogs.com/taiyonghai/p/6728707.html
1、安裝:yum -y install nginx







8、server配置說明
"#"代表注釋,最重要的是server{}塊這部分就代表每一個web站點,此處先暫時設置三個站點
分別使用不同的端口80、81、82保存退出並且重啟nginx
systemctl reload nginx.service
每一個server就是一個虛擬主機,我們有一個當作web服務器來使用
listen 80;代表監聽80端口 server_name xxx.com;代表外網訪問的域名 location / {};代表一個過濾器,/匹配所有請求,我們還可以根據自己的情況定義不同的過濾,比如對靜態文件js、css、image制定專屬過濾 root html;代表站點根目錄 index index.html;代表默認主頁
9、負載均衡配置
負載均衡功能往往在接收到某個請求后分配到后端的多台服務器上,那我們就需要upstream{}塊來配合使用
upstream xxx{};upstream模塊是命名一個后端服務器組,組名必須為后端服務器站點域名,內部可以寫多台服務器ip和port,還可以設置跳轉規則及權重等等 ip_hash;代表使用ip地址方式分配跳轉后端服務器,同一ip請求每次都會訪問同一台后端服務器 server;代表后端服務器地址 server{};server模塊依然是接收外部請求的部分 server_name;代表外網訪問域名 location / {};同樣代表過濾器,用於制定不同請求的不同操作 proxy_pass;代表后端服務器組名,此組名必須為后端服務器站點域名 server_name和upstream{}的組名可以不一致,server_name是外網訪問接收請求的域名,upstream{}的組名是跳轉后端服務器時站點訪問的域名
配置一下Windows的host將我們要訪問的域名aaa.test.com指向Linux
因為硬件有限,我是將Windows中的IIS作為Nginx的后端服務器,所以配置一下IIS的站點域名
打開cmd再ping一下aaa.test.com確實指向Linux系統了,再打開瀏覽器輸入aaa.test.com會顯示bbb這個站點就代表負載成功了。
過程:
打開aaa.test.com,指向了linux服務器地址(10.11.13.22),Nginx通過server的location找到upstream節點,通過ip_hash的策略,導航到IIS服務器(10.11.12.21:80),並顯示內容。
最后推薦一個牛皮的Nginx配置網站,看懂並使用起來需要有一定的基礎。
nginxconfig.io
最后貼一下我的Nginx.conf通用配置模板
# Generated by nginxconfig.io
# https://nginxconfig.io/?0.domain=_&0.path=%2Froot%2Fnginx&0.document_root=%2Fhtml&0.redirect=false&0.https=false&0.php=false&0.index=index.html&0.fallback_html&0.access_log_domain&0.error_log_domain&user=root&file_structure=unified
user root;#centos用戶,看需修改
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 65535;
events {
multi_accept on;
worker_connections 65535;
}
http {
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
log_not_found off;
types_hash_max_size 2048;
client_max_body_size 16M;
# MIME
include mime.types;
default_type application/octet-stream;
# logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
# load configs
include /etc/nginx/conf.d/*.conf;
# _
server {
listen 80;
listen [::]:80;
server_name _;
root /root/nginx/html;#html頁面根目錄,看需修改
# security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
# . files
location ~ /\.(?!well-known) {
deny all;
}
# logging
access_log /var/log/nginx/_.access.log;
error_log /var/log/nginx/_.error.log warn;
# index.html fallback
location / {
try_files $uri $uri/ /index.html;
}
# favicon.ico
location = /favicon.ico {
log_not_found off;
access_log off;
}
# robots.txt
location = /robots.txt {
log_not_found off;
access_log off;
}
# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
expires 7d;
access_log off;
}
# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
# gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
}
}