開(kāi)始編譯
查看Nginx目錄下的東西

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/ conf html logs sbin

配置文件目錄

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/conf/ fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default fastcgi_params koi-win nginx.conf scgi_params.default win-utf

樣例目錄

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/html/ 50x.html index.html

日志目錄

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/logs/

核心進(jìn)程目錄

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/sbin/ nginx

支持-t 檢查語(yǔ)法錯(cuò)誤

[root@aminglinux-02 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

給Nginx創(chuàng)建啟動(dòng)腳本

vim /etc/init.d/nginx

啟動(dòng)腳本內(nèi)容如下:

#!/bin/bash # chkconfig: – 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL

更改配置文件權(quán)限

[root@aminglinux-02 nginx-1.12.1]# chmod 755 !$ chmod 755 /etc/init.d/nginx

將nginx加入到服務(wù)列表里

chkconfig –add nginx

配置開(kāi)啟啟動(dòng)nginx服務(wù)

chkconfig nginx on

定義配置文件 默認(rèn)conf目錄下是有一個(gè)配置文件的,但是我們用自己的配置

[root@aminglinux-02 conf]# ls fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default fastcgi_params koi-win nginx.conf scgi_params.default win-utf [root@aminglinux-02 conf]# mv nginx.conf nginx.conf.old [root@aminglinux-02 conf]# ls fastcgi.conf fastcgi_params.default mime.types nginx.conf.old uwsgi_params fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default fastcgi_params koi-win nginx.conf.default scgi_params.default win-utf

創(chuàng)建一個(gè)配置文件

[root@aminglinux-02 conf]# vim nginx.conf

配置內(nèi)容如下

user nobody nobody; // 定義啟動(dòng)nginx的用戶 worker_processes 2; //定義子進(jìn)程有幾個(gè) error_log /usr/local/nginx/logs/nginx_error.log crit; //錯(cuò)誤日志 pid /usr/local/nginx/logs/nginx.pid; // PID所在 worker_rlimit_nofile 51200; //nginx最多可以打開(kāi)文件的上限 events { use epoll; //使用epoll模式 worker_connections 6000; // 進(jìn)行的最大連接數(shù) } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip \\\’$remote_addr $http_x_forwarded_for [$time_local]\\\’ \\\’ $host "$request_uri" $status\\\’ \\\’ "$http_referer" "$http_user_agent"\\\’; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server //一個(gè)server 對(duì)應(yīng)一個(gè)虛擬主機(jī),定義這個(gè),才能正常訪問(wèn)網(wǎng)站 下面一整段,就是一個(gè)默認(rèn)的虛擬主機(jī) { listen 80; server_name localhost; //網(wǎng)站域名 index index.html index.htm index.php; root /usr/local/nginx/html; //網(wǎng)站的根目錄 location ~ .php$ //配置解析php的部分 { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; //nginx通過(guò)這一行配置來(lái)調(diào)用php-fpm服務(wù) fastcgi_pass 127.0.0.1:9000; //如果php-fpm監(jiān)聽(tīng)的是9000端口,直接這樣寫(xiě)配置即可 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }

作為一個(gè)網(wǎng)站的服務(wù),必須監(jiān)聽(tīng)一個(gè)端口,默認(rèn)監(jiān)聽(tīng)的是80端口,假如沒(méi)有配置 server 這個(gè)幾行,那么nginx將識(shí)別不到監(jiān)聽(tīng)端口,導(dǎo)致服務(wù)不可用
編譯好配置,就使用 -t 檢查一下是否有錯(cuò)

[root@aminglinux-02 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok // /usr/local/nginx/conf/nginx. nginx:配置文件配置語(yǔ)法好 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful // /usr/local/nginx/conf/nginx. nginx:配置文件配置測(cè)試是成功的

啟動(dòng)服務(wù)

[root@aminglinux-02 nginx]# service nginx start Starting nginx (via systemctl): Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. [失敗]

提示出錯(cuò)
之后嘗試執(zhí)行提示的命令 “systemctl status nginx.service ”

[root@aminglinux-02 111]# systemctl status nginx.service ● nginx.service – SYSV: http service. Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled) Active: failed (Result: exit-code) since 四 2017-08-10 21:26:30 CST; 46s ago Docs: man:systemd-sysv-generator(8) Process: 6541 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE) 8月 10 21:26:28 aminglinux-02 nginx[6541]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 8月 10 21:26:28 aminglinux-02 nginx[6541]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 8月 10 21:26:29 aminglinux-02 nginx[6541]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 8月 10 21:26:29 aminglinux-02 nginx[6541]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 8月 10 21:26:30 aminglinux-02 nginx[6541]: nginx: [emerg] still could not bind() 8月 10 21:26:30 aminglinux-02 nginx[6541]: [失敗] 8月 10 21:26:30 aminglinux-02 systemd[1]: nginx.service: control process exited, code=exited status=1 8月 10 21:26:30 aminglinux-02 systemd[1]: Failed to start SYSV: http service.. 8月 10 21:26:30 aminglinux-02 systemd[1]: Unit nginx.service entered failed state. 8月 10 21:26:30 aminglinux-02 systemd[1]: nginx.service failed. Warning: nginx.service changed on disk. Run \\\’systemctl daemon-reload\\\’ to reload units.

提示端口已經(jīng)被占用
查看端口

[root@aminglinux-02 111]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4849/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1236/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2076/master tcp6 0 0 :::3306 :::* LISTEN 1691/mysqld tcp6 0 0 :::22 :::* LISTEN 1236/sshd tcp6 0 0 ::1:25 :::* LISTEN 2076/master

發(fā)現(xiàn)占用端口的就是nginx本身
只能“殺掉 ”nginx程序再試

root@aminglinux-02 111]# killall nginx [root@aminglinux-02 111]# /etc/init.d/nginx start Starting nginx (via systemctl): Warning: nginx.service changed on disk. Run \\\’systemctl daemon-reload\\\’ to reload units. [ 確定 ] [root@aminglinux-02 111]# ps aux |grep nginx root 6623 0.0 0.0 20484 624 ? Ss 21:32 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 6624 0.0 0.1 22928 3216 ? S 21:32 0:00 nginx: worker process nobody 6625 0.0 0.1 22928 3216 ? S 21:32 0:00 nginx: worker process root 6628 0.0 0.0 112664 976 pts/1 R 21:35 0:00 grep –color=auto nginx

這時(shí)發(fā)現(xiàn)啟動(dòng)成功了
但是有警告提示
再次執(zhí)行提示的命令“systemctl daemon-reload ”

[root@aminglinux-02 111]# systemctl daemon-reload [root@aminglinux-02 111]# service nginx stop Stopping nginx (via systemctl): [ 確定 ] [root@aminglinux-02 111]# service nginx start Starting nginx (via systemctl): [ 確定 ] [root@aminglinux-02 111]#

這下就正常了

啟動(dòng)完成以后看一下進(jìn)程

[root@aminglinux-02 111]# ps aux |grep nginx root 6762 0.0 0.0 20484 624 ? Ss 21:45 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 6763 0.0 0.1 22928 3212 ? S 21:45 0:00 nginx: worker process nobody 6764 0.0 0.1 22928 3212 ? S 21:45 0:00 nginx: worker process root 6769 0.0 0.0 112664 972 pts/1 R 21:55 0:00 grep –color=auto nginx

測(cè)試一下訪問(wèn)情況

[root@aminglinux-02 ~]# curl localhost <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>

測(cè)試一下是否支持PHP解析

[root@aminglinux-02 ~]# curl localhost/1.php This is nginx test page.[root@aminglinux-02 ~]# [root@aminglinux-02 ~]# vim /usr/local/nginx/html/1.php [root@aminglinux-02 ~]# curl -x127.0.0.1:80 localhost/1.php -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 16:15:02 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.30

返回碼是200 證明是通的

12.7 默認(rèn)虛擬主機(jī)

修改nginx.cnf配置,刪除默認(rèn)的虛擬主機(jī)配置,重新定義虛擬主機(jī)配置所在路徑

[root@aminglinux-02 default]#vim /usr/local/nginx/conf/nginx.conf user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip \\\’$remote_addr $http_x_forwarded_for [$time_local]\\\’ \\\’ $host "$request_uri" $status\\\’ \\\’ "$http_referer" "$http_user_agent"\\\’; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; include vhost/*.conf; //新增這一行,定義默認(rèn)虛擬主機(jī)的目錄 }

第一次配置的時(shí)候,出現(xiàn)提示 “ } ”這個(gè)所在行語(yǔ)法錯(cuò)誤,一直找不到原因,最后重新粘貼配置文件以后,發(fā)現(xiàn)只是新增的“include vhost/*.conf ”配置最后沒(méi)有添加“ ; ”分號(hào)結(jié)尾。

創(chuàng)建虛擬主機(jī)的目錄

[root@aminglinux-02 conf]# pwd /usr/local/nginx/conf [root@aminglinux-02 conf]# mkdir vhost [root@aminglinux-02 conf]# ls fastcgi.conf fastcgi_params.default mime.types nginx.conf.default scgi_params.default vhost fastcgi.conf.default koi-utf mime.types.default nginx.conf.old uwsgi_params win-utf fastcgi_params koi-win nginx.conf scgi_params uwsgi_params.default 定義新增虛擬主機(jī)的配置 [root@aminglinux-02 conf]# cd vhost/ [root@aminglinux-02 vhost]# pwd /usr/local/nginx/conf/vhost server { listen 80 default_server; //有這個(gè)“default_server ”就是表示這是一個(gè)默認(rèn)虛擬主機(jī) server_name aaa.com; //指定主機(jī)名 index index.html index.htm index.php; //指定索引頁(yè) root /data/wwwroot/default; //指定root的目錄 }

創(chuàng)建網(wǎng)站的根目錄

[root@aminglinux-02 vhost]# mkdir /data/wwwroot [root@aminglinux-02 vhost]# mkdir /data/wwwroot/default [root@aminglinux-02 vhost]# cd !$ cd /data/wwwroot/default [root@aminglinux-02 default]# ls

在“/data/wwwroot/default “下創(chuàng)建索引頁(yè)

[root@aminglinux-02 default]# vim index.html This is the default site.

編輯好之后檢查一下是否有語(yǔ)法的錯(cuò)誤

[root@aminglinux-02 default]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新加載nginx服務(wù)(更改配置文件以后,要重新加載或者重啟一下服務(wù))

[root@aminglinux-02 default]# /usr/local/nginx/sbin/nginx -s reload

測(cè)試一下訪問(wèn)默認(rèn)頁(yè)

[root@aminglinux-02 default]# curl localhost This is the default site. [root@aminglinux-02 default]# ls index.html

因?yàn)樾薷牧薾ginx.conf的配置,現(xiàn)在看到的默認(rèn)索引頁(yè),是我們剛剛新增的vhost的虛擬主機(jī)的索引頁(yè)了

定義默認(rèn)虛擬主機(jī)的兩種辦法:

默認(rèn)虛擬主機(jī),是根據(jù)目錄的第一個(gè).conf了進(jìn)行選擇,所以只需要在vhost目錄下依次創(chuàng)建就可以了,當(dāng)然這種方法不智能 只需要在vhost目錄的.conf配置文件內(nèi),加上一個(gè)“default_server ”即可,把當(dāng)前的這個(gè)配置對(duì)應(yīng)的網(wǎng)站設(shè)置為第一個(gè)默認(rèn)虛擬主機(jī) 12.8 Nginx用戶認(rèn)證

配置Nginx用戶認(rèn)證
新建一個(gè)虛擬主機(jī)配置文件

[root@aminglinux-02 vhost]# vim test.com.conf server { listen 80 ; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / //表示全站,都需要進(jìn)行用戶認(rèn)證 #location /admin // 這個(gè)地方只要加上" /admin " 就變成 針對(duì)這個(gè)站點(diǎn)的“admin” 這個(gè)目錄需要用戶認(rèn)證 #location ~ admin.php //如果把這行這樣寫(xiě),就會(huì)變成,匹配 “ admin.php ”這個(gè)頁(yè)面的時(shí)候才需要用戶認(rèn)證 { auth_basic "Auth"; //定義用戶認(rèn)證的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; //用戶名、密碼文件 } }

虛擬機(jī)創(chuàng)建好之后,創(chuàng)建所需的目錄及文件

[root@aminglinux-02 vhost]# cd /data/wwwroot/ [root@aminglinux-02 wwwroot]# ls default [root@aminglinux-02 wwwroot]# mkdir test.com [root@aminglinux-02 wwwroot]# ls default test.com [root@aminglinux-02 test.com]# vim index.html test.com

配置弄好了,需要生產(chǎn)密碼文件
需要用到Apache生成密碼文件的工具“ htpasswd ”
兩種情況: 1.如果本機(jī)安裝有Apache,可以直接到所在目錄運(yùn)行htpasswd進(jìn)行生成 2.如果沒(méi)有安裝,直接“ yum install -y httpd ”安裝,因?yàn)閥um安裝的,所以工具存放在/usr/bin/下,可以直接使用htpasswd

生成密碼文件

[root@aminglinux-02 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd aming New : Adding password for user aming [root@aminglinux-02 vhost]# cat /usr/local/nginx/conf/htpasswd aming:$apr1$45TTsuN4$9sOnkf8GUOVuCoWI2PcyL/

==關(guān)于htpasswd -c 命令 第一次創(chuàng)建的時(shí)候因?yàn)闆](méi)有htpasswd這個(gè)文件,需要-c創(chuàng)建,第二使用的時(shí)候因?yàn)橐呀?jīng)有這個(gè)htpasswd文件了,將不再需要-c 選項(xiàng),如果還繼續(xù)使用-c 這個(gè)選項(xiàng),將會(huì)重置 htpasswd里的東西==

測(cè)試-c 是否會(huì)重置htpasswd文件

[root@aminglinux-02 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd user1 New : Adding password for user user1 [root@aminglinux-02 vhost]# !cat cat /usr/local/nginx/conf/htpasswd user1:$apr1$I/VKfzgJ$KTWtCG4aZhrvLU69tgvhl1 [root@aminglinux-02 vhost]# htpasswd /usr/local/nginx/conf/htpasswd aming New : Adding password for user aming [root@aminglinux-02 vhost]# !cat cat /usr/local/nginx/conf/htpasswd user1:$apr1$I/VKfzgJ$KTWtCG4aZhrvLU69tgvhl1 aming:$apr1$idtTK3wd$RLibX1IYqH1x.rc6VibVg1

測(cè)試一下語(yǔ)法

[root@aminglinux-02 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新加載服務(wù)

[root@aminglinux-02 vhost]# /usr/local/nginx/sbin/nginx -s reload

==重新加載服務(wù)的好處在于,如果配置里面出錯(cuò),將不會(huì)生效;如果是直接使用restart,如果配置有錯(cuò),將會(huì)直接影響到網(wǎng)站的運(yùn)行==

測(cè)試 location / 針對(duì)整個(gè)站點(diǎn)進(jìn)行用戶認(rèn)證

因?yàn)樾薷呐渲玫臅r(shí)候做的配置,就是針對(duì)整個(gè)站點(diǎn)配置的,直接對(duì)域名進(jìn)行curl即可

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 17:33:37 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth"

提示錯(cuò)誤碼401,需要進(jìn)行認(rèn)證,認(rèn)證方式 Basic realm="Auth"

使用指定用戶測(cè)試

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 17:36:04 GMT Content-Type: text/html Content-Length: 9 Last-Modified: Thu, 10 Aug 2017 17:35:22 GMT Connection: keep-alive ETag: "598c995a-9" Accept-Ranges: bytes 測(cè)試 location /admin 針對(duì)目錄

修改test.com.conf

location /

更改為

location /admin

到站點(diǎn)目錄下加創(chuàng)建一個(gè)admin 的目錄,為了方便測(cè)試,創(chuàng)建一測(cè)試頁(yè)

[root@aminglinux-02 test.com]# pwd /data/wwwroot/test.com [root@aminglinux-02 test.com]# mkdir admin [root@aminglinux-02 test.com]# ls 1.html admin index.html [root@aminglinux-02 test.com]# echo "admin–>test.com–>auth" > admin/login.php [root@aminglinux-02 test.com]# cat admin/login.php admin–>test.com–>auth [root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test.com/admin/login.php -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 17:43:44 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com/admin/login.php -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 17:44:26 GMT Content-Type: application/octet-stream Content-Length: 24 Last-Modified: Thu, 10 Aug 2017 17:41:45 GMT Connection: keep-alive ETag: "598c9ad9-18" Accept-Ranges: bytes 測(cè)試 location ~ 1.html 針對(duì)固定的URL

修改test.com.conf

location /admin

更改為

location ~ 1.html

創(chuàng)建所需的1.html

[root@aminglinux-02 test.com]# pwd /data/wwwroot/test.com [root@aminglinux-02 test.com]# vim 1.html This is the default site. test.com [root@aminglinux-02 test.com]# ls 1.html admin index.html [root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test.com/1.html -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 17:50:19 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com/1.html -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 17:50:42 GMT Content-Type: text/html Content-Length: 35 Last-Modified: Thu, 10 Aug 2017 16:57:21 GMT Connection: keep-alive ETag: "598c9071-23" Accept-Ranges: bytes 12.9 Nginx域名重定向

在Nginx里“ server_name ” 支持跟多個(gè)域名;但是Apache“ server_name ”只能跟一個(gè)域名,需要跟多個(gè)域名,需要使用Alisa
在Nginx的conf配置文件里“server_name ” 設(shè)置了多個(gè)域名,就會(huì)使網(wǎng)站的權(quán)重變了,到底需要哪個(gè)域名為主站點(diǎn)呢
所以需要域名重定向
更改配置“test.com.conf ”文件

server { listen 80 ; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != \\\’test.com\\\’ ) //假如域名,“!=”不等于 test.com,將執(zhí)行下面的腳本 { rewrite ^/(.*)$ http://test.com/$1 permanent; // ^/(.*)$ 正式寫(xiě)法 http://$host/(.*)$ 這段可以直接省略掉的,同時(shí)還可以加上一些規(guī)則,“ permanent ”就是301的意思;如果想弄成302,只需要更改為“ redirect ” } location ~ 1.html { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }

改好配置,檢查語(yǔ)法,重新加載服務(wù)

[root@aminglinux-02 test.com]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful (reverse-i-search)`s\\\’: /usr/local/nginx/^Cin/nginx -t [root@aminglinux-02 test.com]# /usr/local/nginx/sbin/nginx -s reload

測(cè)試test2.com/index.html的跳轉(zhuǎn)情況

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test2.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 18:05:41 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/index.html

可以查看到 location 到了“ http://test.com/index.html ”

測(cè)試test3.com/index.html的跳轉(zhuǎn)情況

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test3.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 18:07:42 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/index.html [root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test3.com/admin/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Thu, 10 Aug 2017 18:07:52 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/admin/index.html

更多關(guān)于云服務(wù)器域名注冊(cè),虛擬主機(jī)的問(wèn)題,請(qǐng)?jiān)L問(wèn)三五互聯(lián)官網(wǎng):m.shinetop.cn

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。郵箱:3140448839@qq.com。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明出處:三五互聯(lián)知識(shí)庫(kù) » 12.6 Nginx安裝 12.7 默認(rèn)虛擬主機(jī) 12.8 Nginx用戶認(rèn)證12.9 Nginx域名重定向

登錄

找回密碼

注冊(cè)

主站蜘蛛池模板: 国产三级精品三级在线看| 国产一区二区日韩在线| 亚洲中文字幕精品无人区| jk白丝喷浆| 日本深夜福利在线观看| 亚洲人成网网址在线看| 亚洲AⅤ天堂AV天堂无码| 丁香五月网久久综合| 免费无遮挡毛片中文字幕| 日韩一卡二卡三卡四卡五卡| 亚洲国产色婷婷久久99精品91| 三人成全免费观看电视剧高清| 亚洲熟女乱一区二区三区| 亚洲人成影院在线观看| 人妻中文字幕不卡精品| 国产av一区二区三区综合| 亚洲日韩成人无码不卡网站| 一区二区三区四区精品视频| 亚洲国产成人久久77| 粉嫩国产av一区二区三区| 国产自产av一区二区三区性色 | 亚洲熟妇无码爱v在线观看 | 免费看黄片一区二区三区 | 色综合久久中文综合久久激情| 国产精品久久国产精麻豆| 真实单亲乱l仑对白视频| 久久日韩精品一区二区五区| 国产一区二区一卡二卡| 晋州市| 伊人色综合久久天天| 一区二区三区精品视频免费播放| 人妻少妇不满足中文字幕| 国产午夜亚洲精品国产成人| 吉川爱美一区二区三区视频| 亚洲一区二区三区小蜜桃| 日韩V欧美V中文在线| 最近中文字幕日韩有码| 国产欧美在线手机视频| 亚洲精品成人A在线观看| 国产av午夜精品福利| 精品国产精品国产偷麻豆|