nginx在Ubuntu下的简易使用


安装

sudo apt-get install nginx

常用控制命令

  1. 启动
    sudo systemctl start nginx
    
  2. 停止
    sudo systemctl stop nginx
    
  3. 重启
    sudo systemctl restart nginx
    
  4. 查看运行状态
    sudo systemctl status nginx
    

配置

nginx配置文件的逻辑

执行:

sudo nginx -t

返回:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

默认的配置文件存放在/etc/nginx/nginx.conf,内容如下:

user www-data;
worker_processes auto;
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 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;
#       }
#}

重点查看

http {
	***此处省略部分内容***
	include /etc/nginx/sites-enabled/*;
}

nginx的配置文件支持嵌套,
/etc/nginx/nginx.conf文件里已经配置已经使用语句include /etc/nginx/sites-enabled/*;
包含了/etc/nginx/sites-enabled/文件夹下所有的文件。
使用以下语句查看该文件夹的内容:

ll /etc/nginx/sites-enabled/

返回:

total 8
drwxr-xr-x 2 root root 4096 Jan 14 22:02 ./
drwxr-xr-x 8 root root 4096 Jan 14 22:05 ../
lrwxrwxrwx 1 root root   28 Jan 14 22:01 default-> /etc/nginx/sites-available/default

可以得出文件/etc/nginx/sites-enabled/default是连接到/etc/nginx/sites-available/default
为方便管理,我们也可以把真正的配置文件放在/etc/nginx/sites-available/目录下,在需要使用的时候使用软连接连接到/etc/nginx/sites-enabled/文件夹下。

新建配置文件

  1. 切换到目录/etc/nginx/sites-enabled/
    cd /etc/nginx/sites-enabled/
    
  2. 删除默认的软连接default
    sudo rm default 
    
  3. 切换到目录/etc/nginx/sites-available/
    cd /etc/nginx/sites-available/
    
  4. 复制一个配置文件
    sudo cp default new_config
    
  5. 编辑配置文件
    编辑一个配置文件,使访问本机80端口的时候访问本机的8080端口。
    server {
            listen 80 default_server;
            listen [::]:80 default_server;
            root /var/www/html;
            server_name _;
    	 	location / {
                    proxy_pass http://127.0.0.1:8080; # 代理到8080端口
                    proxy_set_header Host $http_host; #后台可以获取到完整的ip+端口号
                    proxy_set_header X-Real-IP $remote_addr; #后台可以获取到用户访问的真实ip地址
            }
    }
    

建立软连接

ln -s /etc/nginx/sites-available/new_config /etc/nginx/sites-enabled/new_config

验证配置文件语法

执行:

sudo nginx -t

返回:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

热重载配置文件

sudo nginx -s reload

执行后配置文件立即生效。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值