目录
Nginx是一款高性能、轻量级的wdb服务软件

- #user nobody; #默认运行/管理用户
- worker_processes 1; #工作进程运行数量,可配置成服务器内核数*2,如果网站访问量不大,一般设为1
-
- #error_log logs/error.log; #错误日志文件路径/级别
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
-
- #pid logs/nginx.pid; #pid文件位置

- events { #events:事件
- use epoll; #epoll是一种抗高并发的参数之一
- worker_connections 1024; #每个进程最多处理的连接数量
- }
-
- 进程处理的连接数量有两种修改方式,以下是临时修改
- #如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
- #在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
- #可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.


- http { #http协议的配置
- include mime.types; #文件扩展名与文件类型映射表 用include识别文件
- 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; #此项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
-
- #keepalive_timeout 0; ##连接保持超时时间,单位:秒
- keepalive_timeout 65;
-
- #gzip on; #压缩模块 on 表示开启
-
- server { #web 服务相关的一些配置
- listen 80; #默认监听端口
- server_name localhost; #站点域名
-
- #charset koi8-r; #字符集支持(修改为中文)UTF-8
-
- #access_log logs/host.access.log main; #此web服务的主访问日志
-
- location / { #“/”根目录配置 (浏览器中,www.baidu.com/
- root html; #网站根目录的位置/usr/local/nginx/html(相对路径)
- index index.html index.htm; #支持的首页文件格式
- }
-
- #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;
- }
支持PHP及跳转的配置
- # proxy(代理) the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #}
-
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
-
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
虚拟主机的配置
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
-
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
HTTPS的配置(SSL 加密)
- # HTTPS server
- #
- #server {
- # listen 443 ssl;
- # server_name localhost;
-
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
-
- # ssl_session_cache shared:SSL:1m;
- # ssl_session_timeout 5m;
-
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
-
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- deny IP/IP段:拒绝某个 IP 或 IP段的客户端访问
- allow IP/IP段:允许某个 IP 或IP 段的客户端的访问
- 规则从上往下执行,如匹配则停止,不再往下匹配




nginx支持的虚拟主机
通过 "server {}" 配置段实现
添加域名解析
echo "192.168.226.128 www.accp.com www.benet.com" >> /etc/hosts


准备虚拟站点网页文档
- mkdir -p /var/www/html/accp
- mkdir -p /var/www/html/benet
- echo "
www.accp.com
" >/var/www/html/accp/index.html - echo "
www.benet.com
" >/var/www/html/benet/index.html

修改配置文件
- http {
- ……
- server {
- listen 80;
- server_name www.accp.com;
- charset utf-8;
- access_log logs/accp.access.log;
- location / {
- root /var/www/html/accp;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
-
- server {
- listen 80;
- server_name www.benet.com;
- charset utf-8;
- access_log logs/benet.access.log;
- location / {
- root /var/www/html/benet;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
![]()


重启nginx,测试
systemctl restart nginx



创建8080端口的网页文件
echo " www.accp8080.com
" >/var/www/html/accp8080/index.html

进入配置文件修改
- 进入配置文件 复制www.accp.com的配置,修改端口号
- server { #原accp配置
- listen 192.168.226.128:80; #指向监听端口
- server_name www.accp.com;
- charset utf-8;
- access_log logs/accp.access.log;
- location / {
- root /var/www/html/accp;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
-
- server { #新accp配置
- listen 192.168.226.128:8080; #指向8080端口
- server_name www.accp.com;
- charset utf-8;
- access_log logs/accp8080.access.log; #便于区分,指定生成不同日志
- location / {
- root /var/www/html/accp8080; #指向8080端口的站点首页
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }

重启nginx,测试


添加映射
- 添加192.168.226.100的映射
- vim /etc/hosts
- 192.168.226.100 www.benet.com

创建网站根目录、创建网站首页文件(index.html)
- mkdir /var/www/html/benet100
- echo "
www.benet100.com
" >> /var/www/html/benet100/index.html
临时创建虚拟网卡
ifconfig ens33:0 192.168.226.100 netmask 255.255.255.0

修改配置文件
- server {
- listen 192.168.226.128:80;
- server_name www.accp.com;
- charset utf-8;
- access_log logs/accp.access.log;
- location / {
- root /var/www/html/accp;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
-
- server {
- listen 192.168.226.100:80; #benet监听的IP修改为100
- server_name www.benet.com;
- charset utf-8;
- access_log logs/benet100.access.log;
- location / {
- root /var/www/html/benet100;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }

重启nginx,测试
![]()

