centos7 nginx安裝啟動與配置通用Nginx.conf


參考原文地址:https://www.cnblogs.com/hailang8/p/8664413.html

參考原文地址:https://www.cnblogs.com/taiyonghai/p/6728707.html

 

1、安裝:yum -y install nginx

 

2、安裝成功后nginx的幾個默認目錄
    輸入命令: whereis nginx
    
     執行目錄:/usr/sbin/nginx
     模塊所在目錄:/usr/lib64/nginx/modules
     配置所在目錄:/etc/nginx/
     默認站點目錄:/usr/share/nginx/html
 
     主要配置文件:/etc/nginx/nginx.conf 指向:/etc/nginx/conf.d/default.conf
 
 
     PID目錄:/var/run/nginx.pid
     錯誤日志:/var/log/nginx/error.log
     訪問日志:/var/log/nginx/access.log
 
3、查看nginx狀態(未啟動前)
    命令1:systemctl status nginx.service     
    

 

 
4、啟動、停止、重載命令
    systemctl start nginx.service
    systemctl stop nginx.service
    systemctl reload nginx.service
    systemctl status nginx.service
 
注意:當配置完Nginx.conf並重新啟動Nginx時,需要一定的時間才會生效。
 
5、查看nginx的狀態及進程與端口(啟動后)
    命令1:systemctl status nginx.service
    

 

    
    以上nginx就已安裝成功了!!!
    

 

 
    命令2:netstat -antp | grep :80(查看80端口被哪個服務占用)or netstat -antpuel  | grep ":22" | grep  -v grep(過慮grep本身)
    
 
    命令3:netstat -antp | grep :(查看所有端口占用情況)
    
   
 命令4:ps aux | grep nginx(查看nginx進程運行狀態)or ps aux | grep :80 | grep -v grep(過慮grep本身)
    
 
    查看端口被占用情況
    命令5:lsof -i:端口號
    命令6:netstat -tunlp|grep 端口號
 
6、殺掉進程命令
    a)相關nginx進行全部殺掉:killall -9 nginx  
    b)把PID兩個進程殺掉:kill -9 pid1 and kill -9 pid1 
 
7、查看版本
   命令:nginx -V

 

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;
}
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM