美文网首页
02.Nginx简单配置

02.Nginx简单配置

作者: 和风拂柳花香醉人 | 来源:发表于2018-09-13 11:43 被阅读28次

    前一篇文章已经安装好了Nginx,这一节我们了解为什么能够访问虚拟服务器的80端口就可以访问到Nginx,以及简单学习如何配置Nginx。

    一、配置文件nginx.conf

    配置文件的位置:nginx安装目录/conf/nginx.conf,我的安装目录为/usr/local/nigix/conf/nginx.conf
    打开配置文件:

    
    #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       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   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;
            }
    
            # 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部分,Nginx监听了localhost的80端口,根目录的地址是html/index.htmlhtml/index.htm,错误重定向的页面是html/50x.html,这就是为什么能够通过地址访问Nginx的原因了。

        server {
            listen       80;
            server_name  localhost;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    

    二、配置通过域名访问Nginx

    Nginx的配置文件可以配置多个Server,用来访问多个不同的应用,下面我们配置一个域名为mkh.com的站点,通过端口1234进行访问。

    1、在nginx.conf文件中加入mkh.com的配置

        server {
            listen       1234;
            server_name  mkh.com;
    
            location / {
                root   mkh.com;
                index  home.html;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   mkh.com;
            }
        }
    

    2、在Nginx的安装目录下创建文件夹mkh.com,并且创建文件home.html50x.html

    $ cd /usr/local/nginx
    $ mkdir mkh.com
    $ cd mkh.com
    $ touch home.html
    $ touch 50x.html
    

    home.html50x.html的内容随便写。

    3、重启Nginx

    $ /usr/local/nginx/sbin/nginx -s reload
    

    4、配置防火墙,开放1234端口,重启防火墙

    $ vim /etc/sysconfig/iptables
    
    #加入规则
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 1234 -j ACCEPT
    
    
    $ /etc/init.d/iptables restart
    

    5、修改真实机器的hosts文件,配置mkh.com的映射地址

    注意:这一步是在真实机器上操作,不是虚拟机。

    由于没有DNS,mkh.com可能会映射到互联网中的其他IP地址,所以我们要修改当前机器的hosts文件,让它映射到我们指定的IP地址。

    $ sudo vim /etc/hosts
    
    #加入下面这一行
    192.168.3.180 mkh.com
    
    
    $ sudo service networking restart
    

    6、打开浏览器访问 mkh.com:1234

    1.png

    配置成功。

    相关文章

      网友评论

          本文标题:02.Nginx简单配置

          本文链接:https://www.haomeiwen.com/subject/ptlwgftx.html