参考原文地址: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;
}
}

 

 
posted on 2019-08-16 10:32  jeffh  阅读(27976)  评论(0编辑  收藏  举报