基于Ngnix的静态网页开发学习

主要参考文章:
http://www.360doc.com/content/17/1209/17/27831725_711573929.shtml
https://www.cnblogs.com/feige1314/p/7111801.html
https://cloud.tencent.com/developer/article/133108
需要工具Dreamviewer,用来index.html的排版和设计;腾讯云服务器,之前已经安装和配置了ngnix,还有以上的参考文档。

之前虽然在腾讯云上配置了ngnix,也成功了,但是当我将自己设计的index.html,替换/usr/local/src/nginx-1.6.2/html 下的index.html,也修改了ngnix.conf时,仍然显示不出我的m.html,不过这已经不重要了,我又尝试了另外一种方法,在ngnix上配置静态网页。主要参考这篇文章(https://www.cnblogs.com/feige1314/p/7111801.html)
重新以root的方式安装ngnix

yum install nginx -y
ngnix #直接启动ngnix
netstat -ntalp | grep nginx#查看nginx运行状态
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      16902/nginx: worker
tcp6       0      0 :::80                   :::*                    LISTEN      16902/nginx: worker

修改默认路径:打开 Nginx 的默认配置文件 /etc/nginx/nginx.conf ,修改 Nginx 配置,将默认的 root /usr/share/nginx/html; 修改为: root /data/www;,如下:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /data/www; ##已修改

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }
 
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

然后在/data下创建www/文件夹,将index.html以及images添加到www/文件夹下,在你的浏览器中输入http://ip地址/index.html,即可显示index.html中的内容。但是index.html中的图片还无法显示,由于index.html与images文件夹在同一个目录,这时候需要修改下 /etc/nginx/nginx.conf,才能将images中的图片显示出来,如下:

       location /images/{
        root    /data/www/;
        autoindex on;
        }

将这段配置文件输入配置文件中(位置一般位于location /{}下一段,放置于别处没试过),/data/www/即为images的路径,这样再重新录入http://ip地址/index.html,即可显示图文信息了。

你可能感兴趣的:(基于Ngnix的静态网页开发学习)