1、 ubuntu16.04下安裝Nginx(通過ubuntu安裝源進行安裝)
$:sudo apt-get update
$:sudo apt-get install nginx
安裝完成之後,通過:
$:nginx -v 可檢視nginx軟體版本
到此安裝成功。說明:如果安裝網絡環境無法FQ,則可通過修改ubuntu安裝源,再執行update和install操作。修改安裝源具體步驟如下:
a、安裝源原檔案備份:
$:sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
b、編輯源清單檔案:
$:sudo vim /etc/apt/sources.list
c、将該檔案内容删除,然後添加如下内容(三者選其一):
阿裡雲安裝源:
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
或者:中科大安裝源:
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
或者:網易163安裝源:
deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
2、Nginx配置檔案修改
a、編輯nginx.conf配置檔案:
$:vim /etc/nginx/nginx.conf
檔案内容如下:
user www www;
#user nobody nobody;
worker_processes 4;
#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;
client_max_body_size 100M;
#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;
# }
# location ~ \.php {
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index /index.php;
# include /usr/local/nginx/conf/fastcgi_params;
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_param PATH_INFO $fastcgi_path_info;
# fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# }
#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;
#}
#}
include ./vhosts/*.conf;
# 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;
# }
#}
}
b、在nginx檔案目錄下添加vhost檔案目錄:
/etc/nginx$:mkdir vhosts
c、在vhosts檔案下添加serverA.conf、serverB.conf(根據實際需要,配置多少個域名就配置多少個.conf檔案)
示例1:
/etc/nginx/vhosts$:vim serverA.conf
編輯内容如下:
server {
listen 80;
server_name www.A.com; #綁定域名
index index.htm index.html index.php; #預設檔案
root /opt/www/Aserver/dist/; #網站根目錄路徑
#include location.conf; #調用其他規則,也可去除
location / {
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Credentials' "true";
try_files $uri $uri/ /index.php?$query_string;
}
location /api {
proxy_set_header Host $proxy_host;
proxy_pass http://api.D.com; #配置的接口域名
proxy_buffer_size 128k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.html; #網站根目錄路徑下的index.htm index.html index.php,根據實際已有檔案去填
include /etc/nginx/fastcgi_params; #根據實際nginx安裝路徑去填寫
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
示例2:
/etc/nginx/vhosts$:vim serverB.conf
編輯内容如下:
server {
listen 80;
server_name www.B.com api.D.com; #綁定域名(綁定2個域名,中間空格間隔開,api開頭的可作為接口域名:見示例1)
index index.htm index.html index.php; #預設檔案
root /opt/www/Bserver/public/; #網站根目錄
location / {
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Credentials' "true";
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php; #網站根目錄路徑下的index.htm index.html index.php,根據實際已有檔案去填
include /etc/nginx/fastcgi_params; #根據實際nginx安裝路徑去填寫
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
示例3:
/etc/nginx/vhosts$:vim serverC.conf
編輯内容如下:
server {
listen 80;
server_name www.C.com; #綁定域名
index index.htm index.html index.php; #預設檔案
root /opt/www/Cserver/dist/; #網站根目錄
#include location.conf; #調用其他規則,也可去除
location / {
add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Credentials' "true";
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.html; #網站根目錄路徑下的index.htm index.html index.php,根據實際已有檔案去填
include /etc/nginx/fastcgi_params; #根據實際nginx安裝路徑去填寫
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
說明:示例1、2、3中,其中3最為簡單,配置單任意域名網站;示例1示例2中,示例2的其中一個域名作為示例1的接口域名,友善示例1調用示例2的接口。【在配置時根據實際需要需配置自己所需的域名】
d、配置好nginx代理伺服器後,在nginx/sbin目錄下使用如下指令:
nginx/sbin$:./nginx -t #測試是否成功
nginx/sbin$:./nginx -s reload #重新開機nginx服務
e、在/etc/hosts檔案中,編輯添加配置的域名:
IP www.A.com
IP www.B.com
IP www.C.com
IP api.D.com
說明:IP指部署網站所在的伺服器的IP位址,如網站部署在192.168.5.10上,則IP就設為該IP位址
3、Dnsmasq伺服器配置
a、安裝Dnsmasq:
$:sudo apt-get install dnsmasq
b、編輯dnsmasq的配置檔案:
$:sudo vi /etc/dnsmasq.conf
# or if you want it to read another file, as well as /etc/hosts, use
# this.
#addn-hosts=/etc/banner_add_hosts
addn-hosts=/etc/hosts #将此行取消注釋
b、重新開機Dnsmasq服務:
$:systemctl restart dnsmasq
4、測試區域網路是否能正常通路(兩種方法)
方法a、将所有區域網路内的用戶端網絡DNS均設為Dnsmasq伺服器IP位址 打開網絡和共享中心->本地連接配接->屬性->IPV4->使用下面的DNS伺服器位址,将Dnsmasq伺服器IP位址填入 浏覽器分别打開
www.A.com
www.B.com
www.C.com
api.D.com
觀察能否正常通路。
方法b、所有區域網路用戶端電腦組態host檔案 計算機->C槽(系統盤鎖在位置)->windows->System32->drivers->etc,編輯添加hosts檔案,内容同2中的e,完成後儲存。
IP www.A.com
IP www.B.com
IP www.C.com
IP api.D.com
說明:IP指部署網站所在的伺服器的IP位址,如網站部署在192.168.5.10上,則IP就設為該IP位址 浏覽器分别打開www.A.com;www.B.com;www.C.com;api.D.com,觀察能否正常通路
posted on 2019-03-11 12:23 Calainkey 閱讀(...) 評論(...) 編輯 收藏