記一次使用nginx部署靜態網站

由於拿到的服務器已經安裝好nginx了,訪問服務器ip可以看到nginx的歡迎頁。
在這裏插入圖片描述
那麼直接配置映射靜態網站就好了。

1.查找nginx安裝目錄:

使用命令:whereis nginx
在這裏插入圖片描述
可以看到nginx的安裝目錄爲: /alidata/server/nginx-1.4.4/
進入此目錄下,查看nginx的配置文件。
在這裏插入圖片描述
上圖的nginx.conf即爲nginx默認的配置文件名稱。如果自定義了配置文件,需要在啓動的時候,後面加上自定義的文件路徑。

由於這裏不清楚是不是使用的這個文件,那麼先看下這個文件的內容:cat nginx.conf

[root@iZm5ebk32d2s767i2tebe5Z conf]# cat nginx.conf

user  www www;
worker_processes  1;

error_log  /alidata/log/nginx/error.log crit;
pid        /alidata/server/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process. 
worker_rlimit_nofile 65535;

events 
{
  use epoll;
  worker_connections 65535;
}


http {
        include       mime.types;
        default_type  application/octet-stream;

        #charset  gb2312;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 8m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        #limit_zone  crawler  $binary_remote_addr  10m;
        log_format '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
        include /alidata/server/nginx/conf/vhosts/*.conf;
}

可以看到,上面最後一行表示此配置文件包含了/alidata/server/nginx/conf/vhosts/目錄下的所有的conf結尾的文件。

爲了避免此次修改影響到別的配置,這裏我們新建一個配置文件testRocWind.conf

[root@iZm5ebk32d2s767i2tebe5Z conf]# cd  /alidata/server/nginx/conf/vhosts/
[root@iZm5ebk32d2s767i2tebe5Z vhosts]# ls
ajrtest.conf  default.conf.bak  img.conf  phpmyadmin.conf  phpwind.conf  pms.conf  testRocWind.conf  www.conf  zadmin.conf

看下文件內容:

[root@iZm5ebk32d2s767i2tebe5Z vhosts]# cat testRocWind.conf
server {
    listen 80;             # 端口號
    server_name rocwind.xxxxxxxxx.com; # 配置域名信息
    location / { 
        root /var/www/;               # 靜態頁面根目錄  # 訪問路徑爲/時 到 /var/www/下找文件 
        index index.html;
    }
}

端口的話,這裏還是監聽的是80端口,域名,這裏配上自己的域名即可,但是需要是已備案過的域名,否則是請求不通的。

由於靜態網站的首頁index.html在服務器上的位置爲/var/www/index.html,所以這裏需要在location下面的root上配置路徑信息。

其中這裏位置的映射使用的是root,還可以使用alias。參考:nginx靜態文件映射root和alias

alias與root區別:

Sets the root directory for requests. For example, with the following configuration
location /i/ {
    root /data/w3;
}

The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request

Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
    alias /data/w3/images/;
}

on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

當訪問/i/top.gif時,root是去/data/w3/i/top.gif請求文件,alias是去/data/w3/images/top.gif請求,也就是說

root響應的路徑:配置的路徑+完整訪問路徑(完整的location配置路徑+靜態文件)
alias響應的路徑:配置路徑+靜態文件(去除location中配置的路徑)

需要注意的是alia配置的目錄後必須加 /

通常情況下 location / 配置中用 root, location /other 使用alias

2.測試配置文件是否能正確加載

我這裏使用的是nginx默認的配置文件。

[root@iZm5ebk32d2s767i2tebe5Z vhosts]# /alidata/server/nginx-1.4.4/sbin/nginx -t
nginx: the configuration file /alidata/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /alidata/server/nginx/conf/nginx.conf test is successful

-c:使用指定的配置文件而不是conf目錄下的nginx.conf 。
-t:測試配置文件是否正確,在運行時需要重新加載配置的時候,此命令非常重要,用來檢測所修改的配置文件是否有語法錯誤。
-s:reload 重載
-s:stop 停止

如果使用了自定義的配置文件,則需要在後面加上 -c 配置文件路徑
如:/alidata/server/nginx-1.4.4/sbin/nginx -t -c /alidata/server/nginx/conf/nginx.conf

測試配置文件正確了,下一步就是重新加載nginx,使修改的配置文件生效。

[root@iZm5ebk32d2s767i2tebe5Z vhosts]# /alidata/server/nginx-1.4.4/sbin/nginx -s reload -c /alidata/server/nginx/conf/nginx.conf
[root@iZm5ebk32d2s767i2tebe5Z vhosts]# 

不出意外的話,運行之後,控制檯什麼信息也不會輸出,配置也會立即生效了。

下面可以測試一下,訪問配置文件裏面配置的域名,是否替換了nginx的默認歡迎頁。
在這裏插入圖片描述
如果配置錯誤的話,訪問域名,仍然會顯示nginx的默認歡迎頁。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章