1.添加nginx的安裝源html
vi /etc/yum.repos.d/nginx.repo
2.輸入下面內容,並保存退出node
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
這裏是RHEL7nginx
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/rhel/7/$basearch/
gpgcheck=0
enabled=1
3.安裝git
yum install nginx
4.啓動github
[root@freesaber tmp]# systemctl start nginx
[root@freesaber tmp]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2019-03-28 16:16:38 CST; 7s ago
Docs: http://nginx.org/en/docs/
Process: 6538 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 6539 (nginx)
CGroup: /system.slice/nginx.service
├─6539 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─6540 nginx: worker process
下面安裝nodeweb
1.安裝nvm https://github.com/creationix/nvmdocker
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
2.進入nodejs官網,查看當前nodejs的版本shell
3.使用nvm安裝nodenpm
nvm install 10.15.3
4.指定node的版本centos
[root@freesaber ~]# nvm use v10.15.3
Now using node v10.15.3 (npm v6.4.1)
[root@freesaber ~]# nvm alias default v10.15.3
default -> v10.15.3
5.查看版本
[root@freesaber ~]# node -v
v10.15.3
[root@freesaber ~]# npm -v
6.4.1
6.編寫一段node.js腳本,並運行
vi app.js
const http = require('http')
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plan;charset=utf-8'});
res.end('來自8081端口的node響應')
}).listen(8081);
node app.js
7.在阿里雲的安全組中添加端口入規則
8.訪問
下面是個人兩個域名,默認都是訪問的服務器的80端口
http://www.freesaber.cn/
http://nodetree.freesaber.cn/
因爲沒有配置nginx訪問這兩個域名,都會獲得同一個返回頁面
下面將個人二級域名nodetree.freesaber.cn反向代理到個人8081端口,新開一個shell遠程窗口,不要關閉node
1.nginx的目錄結構和配置文件,咱們新增的配置放在conf.d目錄下
[root@freesaber nginx]# cd /etc/nginx
[root@freesaber nginx]# ll
total 40
drwxr-xr-x 2 root root 4096 Mar 28 16:14 conf.d
-rw-r--r-- 1 root root 1007 Mar 26 22:27 fastcgi_params
-rw-r--r-- 1 root root 2837 Mar 26 22:27 koi-utf
-rw-r--r-- 1 root root 2223 Mar 26 22:27 koi-win
-rw-r--r-- 1 root root 5231 Mar 26 22:27 mime.types
lrwxrwxrwx 1 root root 29 Mar 28 16:14 modules -> ../../usr/lib64/nginx/modules
-rw-r--r-- 1 root root 643 Mar 26 22:25 nginx.conf
-rw-r--r-- 1 root root 636 Mar 26 22:27 scgi_params
-rw-r--r-- 1 root root 664 Mar 26 22:27 uwsgi_params
-rw-r--r-- 1 root root 3610 Mar 26 22:27 win-utf
[root@freesaber nginx]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
2.複製一份默認的配置文件
[root@freesaber nginx]# cd /etc/nginx/conf.d
[root@freesaber conf.d]# ls
default.conf
[root@freesaber conf.d]# cp default.conf nodetree.conf
[root@freesaber conf.d]# ll
total 8
-rw-r--r-- 1 root root 1093 Mar 26 22:25 default.conf
-rw-r--r-- 1 root root 1093 Mar 28 16:58 nodetree.conf
[root@freesaber conf.d]#
3.修改內容新增的nodetree.conf,並保存退出
server { listen 80; server_name nodetree.freesaber.cn; location / { # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; # proxy_set_header Host $http_host; # proxy_set_header X-Nginx-Proxy true; proxy_pass http://127.0.0.1:8081;
# proxy_redirect off;
}
}
4.重啓nginx,訪問二級域名,這時請求被反向代理到了8081端口的node服務,而對外的服務都是nginx的80端口。其實這個時候,能夠從阿里雲的安全組中刪除8081的入規則。由於nginx代理到本地的8081,而8081不會被外部訪問。
補充docker:
https://www.runoob.com/docker/docker-install-nginx.html
其中第一次運行nignx是爲了拿到默認配置文件,到指定目錄(多拷貝一個conf.d)。須要添加一個反向代理的目錄。
docker run -d -p 80:80 --name nginx-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx -v ~/nginx/conf.d:/etc/nginx/conf.d nginx
nginx使用容器部署,而後其餘應用也使用ngxin部署後,反向代理的ip不能使用127.0.0.1。能夠經過docker inspect來查看容器內部的ip地址,能夠經過此ip地址+容器內的端口號進行代理。
添加的反向代理conf
server {
listen 80;
server_name huangyielm.freesaber.cn;
location / {
proxy_pass http://172.17.0.2:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}