Nginx的安装与初步使用

时间:2020-04-11
本文章向大家介绍Nginx的安装与初步使用,主要包括Nginx的安装与初步使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Nginx的安装与初步使用

Nginx的安装与初步使用

1. Nginx简介


nginx

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。

其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

2. Nginx的安装

2.1 Linux环境

直接yum命令安装即可,不用下载源码编译

# 在阿里云的控制台直接yum安装即可
yum install nginx

其他安装方法可参见菜鸟教程

安装完成之后可以通过如下命令来验证是否安装成功

#查看Nginx版本
nginx -v
#查看Nginx安装位置
whereis nginx

检查配置

2.2 Windows 环境

在Nginx官网下载Windows版本软件包。


Nginx下载

3. Nginx的配置文件

Nginx的配置主要是修改nginx.conf文件,手动安装的在安装目录下面,自动安装的在/etc/ngixn目录下面。

# 修改配置文件
vim /etc/nginx/nginx.conf

Nginx的默认配置文件:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    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;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       9527;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   D:/blog/public;
            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;
        }

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

}

重点需要关注其中的server配置:


server配置
  1. listen就是访问端口,默认80,我这里修改为8010;

  2. server_name是服务名称,可以写自己的域名,我这里填写本地localhost;

  3. location是指定资源名称,其中root是指定主页目录地址,index指定主页文件名称,这里配置的可以是index.html或者index.htm

4. 启动Nginx,访问页面

在启动Nginx前需要通过名称验证配置文件是否正确

# 验证配置文件
nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

启动nginx,启动之后就可以通过地址和端口好访问查看nginx是否已经配置成功

# 启动nginx
nginx

5. Nginx的其他常用命令

# 重新载入配置文件
nginx -s reload

# 重启 Nginx
nginx -s reopen

# 停止 Nginx
nginx -s stop

原文地址:https://www.cnblogs.com/alltoforever/p/12680886.html