Ubuntu搭建Nginx服务器

安装Nginx

输入命令,安装nginx服务器

sudo apt-get install nginx

输入y确认,等待安装完成

在浏览器输入localhost,可以看到nginx欢迎页面

在这里插入图片描述

配置文件

查看配置文件 cat /etc/nginx/nginx.conf

配置文件解析参考:https://www.jianshu.com/p/a145a1bc60df

全局配置文件

#运行用户,默认即是nginx,可以不进行设置
user www-data;

#Nginx进程,一般设置为和CPU核数一样
worker_processes auto;

#进程pid存放位置
pid /run/nginx.pid;

include /etc/nginx/modules-enabled/*.conf;


events {
		# 单个后台进程的最大并发数
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

		#开启高效传输模式
        sendfile on;
        
        tcp_nopush on;
        tcp_nodelay on;
		
		#保持连接的时间,也叫超时时间
        keepalive_timeout 65;
        
        types_hash_max_size 2048;
        
        # server_tokens off;
        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

		#文件扩展名与类型映射表
        include /etc/nginx/mime.types;
		
		#默认文件类型
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

		#开启gzip压缩
        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

		#包含的子配置项位置和文件
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
# 
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

子配置文件管理

sites_availables和sites_enabled

在sites_availables和sites_enabled下均有default配置文件,但实际只引用了sites_enabled中的配置;

include /etc/nginx/sites-enabled/*;

sites_availables 指的是所有 nginx 站点;
sites_enabled 指的是当前开启的 nginx 站点;

当需要增加网站时,在sites_availables 里增加对应的配置文件,当需要启用这个网站的时候,在sites_enabled 创建对应配置文件的软连接即可

ln -s /etc/nginx/sites-available/local.conf  /etc/nginx/sites-enabled/local.conf 

文件管理资料参考:https://blog.csdn.net/hanziyuan08/article/details/104031959

default配置文件

//文件路径
/etc/nginx/sites_enabled/default

//文件内容说明

server {
    listen       80;   #配置监听端口
    server_name  localhost;  //配置域名

    #charset koi8-r;     
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;     #服务默认启动目录
        index  index.html index.htm;    #默认访问文件
    }

    #error_page  404              /404.html;   # 配置404页面

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;   #错误状态码的显示页面,配置后需要重启
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

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

启动、停止、重启Nginx

启动

nginx    //后续重启的话,只能通过强制删除进程,才能成功
//或者
systemctl start nginx.service

停止

//三选一
nginx -s quit    //温和停止
nginx -s stop    //强硬停止
killall nginx    //野蛮停止,杀死进程
//或者
systemctl stop nginx.service

重启

systemctl restart nginx.service

查询

ps aux | grep nginx

其他设置

自定义错误页

error_page   状态码  错误页路径;

访问权限

//allow:允许ip访问
//deny:禁止ip访问
//all:所有ip
//使用all会覆盖后者控制的权限(如:deny all在前,allow xxx.xxx.xxx.xxx在后,表示禁止所有ip访问)
#根目录权限
location / {
    allow xxx.xxx.xxx.xxx;
    deny all;
}

#子目录权限
location =/img{
    allow all;
}
location =/admin{
    deny all;
}

#使用正则,控制.php文件访问权限
location ~\.php$ {
    deny all;
}

gzip压缩

gzip是网页的一种网页压缩技术,经过gzip压缩后,页面大小可以变为原来的30%甚至更小。更小的网页会让用户浏览的体验更好,速度更快。gzip网页压缩的实现需要浏览器和服务器的支持。

http {
    gzip on;
    gzip_types text/plain application/javascript text/css;
}
配置项作用
gzip该指令用于开启或 关闭gzip模块。
gzip_buffers设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
gzip_comp_levelzip压缩比,压缩级别是1-9,1的压缩级别最低,9的压缩级别最高。压缩级别越高压缩率越大,压缩时间越长
gzip_disable可以通过该指令对一些特定的User-Agent不使用压缩功能
gzip_min_length设置允许压缩的页面最小字节数,页面字节数从相应消息头的Content-length中进行获取
gzip_http_version识别HTTP协议版本,其值可以是1.1.或1.0
gzip_proxied用于设置启用或禁用从代理服务器上收到相应内容gzip压缩
gzip_vary用于在响应消息头中添加Vary:Accept-Encoding,使代理服务器根据请求头中的Accept-Encoding识别是否启用gzip压缩

引用

简栋梁 Nginx教程 https://www.jianshu.com/p/a145a1bc60df
Bpazy 对ubuntu nginx 默认目录的理解 https://blog.csdn.net/hanziyuan08/article/details/104031959
Nginx配置详解 https://www.cnblogs.com/knowledgesea/p/5175711.html#!comments

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值