使用Nginx + Node.js部署你的网站(转)

news/2024/5/19 7:21:54/文章来源:https://blog.csdn.net/weixin_34224941/article/details/93436356

转自:https://www.jianshu.com/p/717f2b88d057

Nginx是一个高性能的HTTP和反向代理服务器(反向代理就是通常所说的web服务器加速,它是一种通过在繁忙的web服务器和internet之间增加一个高速的web缓冲服务器来降低实际的web服务器的负载),Nginx由俄罗斯程序员利用C语言开发,以稳定、低系统资源消耗闻名,腾讯、百度、阿里、京东、网易等均有部署使用。此外,在高连接并发的情况下,Nginx是Apache的不错替代品,其能够支持高达50000个并发连接数的响应。

一、Nginx在Linux下的安装

1、编译工具和库文件的安装

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

2、prce的安装
以下假设我们安装在src文件夹中
下载:

[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

解压:

[root@bogon src]# tar zxvf pcre-8.35.tar.gz 

安装:

cd pcre-8.35./configure
make && make install

3、Nginx的安装
下载:

[root@bogon src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

安装:

[root@bogon nginx-1.6.2]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[root@bogon nginx-1.6.2]# make [root@bogon nginx-1.6.2]# make install 

最后进入响应的目录执行nginx可执行文件即可。

二、Nginx在Windows下的安装

Windows下只需要下载解压即可使用,下载地址http://nginx.org/en/download.html
运行nginx.exe,即可启动服务,在浏览器中打开可看到以下画面,这说明Nginx已经运行起来了。

三、Nginx的配置

要运行起自己的网站我们还需要对Nginx做一些配置,在nginx文件夹的子文件夹conf下的nginx.config文件就是Nginx的配置文件,其文件内容如下:


#user  nobody;
worker_processes  1;#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; #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; } #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; #} } # 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; # } #} } 

#显然代表注释,以下是这些配置的一些说明

#使员工Nginx的用户名
#user  nobody;#cpu数,一般设置成和服务器的cpu数一致
worker_processes  1;#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #进程id #pid logs/nginx.pid; events { worker_connections 1024; } http { #设置mime类型,类型由mime.type文件定义 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指令指定Nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设定为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime sendfile on; #tcp_nopush on; #设置超时时间 #keepalive_timeout 0; keepalive_timeout 65; #是否开启gzip压缩(网页速度优化非常有用,开启后通常可以达到70%的压缩率) #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; } #定义错误提示页面,你还可以在这里添加500,403等,以空格分开 #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; #} } # 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; # } #} } 

四、Nginx + Node.js部署

在Nginx中添加一个server类,如下:

server {listen       80;#域名server_name  huruji3.com www.huruji3.com;location / {#node.js应用的端口proxy_pass http://127.0.0.1:3000; root blog; } #静态文件交给Nginx直接处理 location ~ *^.+\.(css | js | txt | swf | mp4)$ { root E:\huruji\blog\wechat_v1.1\public; access_log off; expires 24h; } } 

当然,我们为了最大化的利用域名,我们有时需要更多的使用二级域名,以运行更多的应用,同样我们只要再添加一个类:

server {listen       80;#域名server_name  blog.huruji3.com;location / {#node.js应用的端口proxy_pass http://127.0.0.1:3001; root blog; } #静态文件交给Nginx直接处理 location ~ *^.+\.(css | js | txt | swf | mp4)$ { root E:\huruji\blog\wechat_v1.1\public; access_log off; expires 24h; } } 

这里说明一下,我们利用二级域名是一种充分利用的域名资源的方法,同样利用路径也可以,这和使用的服务器内部采用的映射方式有关,比如院网和工作室网站对外表现就是不同的网站,但是工作室网站的/hope只是一个路径而已,Nginx不能根据路径,可以使用二级域名使得不同应用运行在同一个一级域名下。
以下的Nginx配置,打开不同域名也就访问了不同网站:


#user  nobody;
worker_processes  1;#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; gzip on; server { listen 80; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # server { listen 80; server_name huruji3.com www.huruji3.com; location / { proxy_pass http://127.0.0.1:3000; root blog; } #location ~ *^.+\.*$ { # root E:\huruji\blog\wechat_v1.1\public; # access_log off; # expires 24h; #} } server { listen 80; server_name blog.huruji3.com; location / { proxy_pass http://127.0.0.1:3001; root blog; } #location ~ *^.+\.*$ { # root E:\huruji\blog\wechat_v1.1\public; # access_log off; # expires 24h; #} } # 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; # } #} } 

浏览器输入不同地址,也就访问了不同网站应用:



作者:忽如寄
链接:https://www.jianshu.com/p/717f2b88d057
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

转载于:https://www.cnblogs.com/mersn/p/10511040.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_768071.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

phpcms网站搬家 至 服务器 完整并且详细过程

想要自己的网站让人们看到那就要上传服务器空间后,这样才会通过搜索域名进行网页访问。 但是在上传的过程中肯定会有很多东西要修改,例如数据库怎么上传、路径怎么修改等。。。。。这些对于生手可能会慌乱,不用害怕! 这就让大家看…

制作chrome插件/扩展程序,禁止谷歌浏览器访问某些网站

简单地说,浏览器插件,可以大大的扩展你的浏览器的功能。包括但不仅限于这些功能: 捕捉特定网页的内容捕捉HTTP报文捕捉用户浏览动作,改变浏览器地址栏/起始页/书签/Tab等界面元素的行为与别的站点通信,修改网页内容………

把你开发的网站免费发布到互联网上

2019独角兽企业重金招聘Python工程师标准>>> 0. 前言 之前我们写过很多代码,但几乎都是在自己的电脑上运行的。如果别人要看,也只能在电脑上演示,或者把代码发给他运行。 而在学习 web 开发,比如 Django、Flask、webpy…

营销运作百科:全国公安机关互联网网站安全服务平台备案流程

最近速名网企业建站网站备案完成了。当然,作为一家合法经营的企业网站,建站之后肯定是要去公安系统备案的。现在国家也强制使用国内服务器的用于需要在规定时间30天内在全国互联网安全管理服务平台进行备案,理由其实很简单,因为防…

茶陵SEO优化网站关键词排名如何做才能上首页

今天速名网的主要话题就是围绕如何做好关键词“茶陵SEO”、“茶陵SEO优化”、“茶陵网站关键词排名”、“茶陵SEO关键词排名”、“茶陵关键词排名”的排名而设定的。你是否注意到从标题上,速名网就对关键词进行了拆分与组合,这就是SEO的魅力所在。它不具…

SEO网络营销培训机构都是如何给新手SEO培训的,新手SEO看完就知道套路,以后就不会上当受骗

SEO网络营销培训机构都是如何给新手SEO培训的?如题,今天速名网就来给大家聊聊SEO培训这个话题。诚然,SEO网络营销听起来就很高大上,但是在如今各大平台圈流量形成闭环的情况对于SEO行业来说都是一个寒冬。尤其是短视频营销&#x…

新开网站不收录的原因及解决方法有哪些

新开一个网站发现一个月了收录很少,那么新开网站不收录的原因及解决方法有哪些呢?速名网长期研究SEO优化,尤其是百度SEO关键词排名技术与算法,在实战中,我们总结了这样一条经验。在等待中爆发,做好原创内容…

网站域名对SEO优化的重要性分析

域名重要吗?绝对地。选择正确的域名是您营销策略的重要组成部分。您应该选择一个适合您公司品牌并且不太复杂的域名。名称本身也不是URL中唯一的考虑因素。公司应该考虑TLD(顶级域)部分。如果您的域是.com、.org或.computers(或其…

SEO优化百科:安防监控行业网站SEO优化关键词排名如何做

安防监控行业网站SEO优化关键词排名如何做?关于监控摄像头行业的关键词排名,速名网小编做这行业推广是相当有经验的。对于监控行业的推广模式,我们能够采取哪些手段来进行推广呢。 第一、确定关键词。一般而言,首先我们需要确定主关键词&am…

SEO百科:B2B平台SEO优化关键词排名如何做

在为B2B公司做SEO时,重要的是要记住,客户的交易过程比B2C长得多。这些购买规模更大,并且有更多的利益相关者参与了购买过程。因此,当您进行关键字研究时,您需要在整个消费者的过程中考虑关键字,将金字塔顶部…

国仁老猫:抖音影视剪辑5种赚钱方法、18个素材网站、8.0剪辑方法防判搬运(建议收藏)

2020年2月,毒舌电影COO、“毒sir”本人陈植雄在接受采访时说:“抖音影视号还是片蓝海”。 彼时,剪辑影视号在抖音还波澜未起。 半年过去,以毒舌电影(4105.7W粉丝)、贤于葛格(1986.9w粉丝&…

H5手机网站中插入天气预报代码,兼容手机和PC网站

效果如图 代码使用方法 <iframe scrolling"no" src"https://tianqiapi.com/api.php?styleya&skincucumber" frameborder"0" width"320" height"200" allowtransparency"true"></iframe>

semcms 网站漏洞挖掘过程与安全修复防范

2019独角兽企业重金招聘Python工程师标准>>> emcms是国内第一个开源外贸的网站管理系统&#xff0c;目前大多数的外贸网站都是用的semcms系统&#xff0c;该系统兼容许多浏览器&#xff0c;像IE,google,360极速浏览器都能非常好的兼容&#xff0c;官方semcms有php版…

quickGO:在线查询GO和GO注释信息的网站

欢迎关注”生信修炼手册”!quickGO是EMBL-EBI发布的网站&#xff0c;通过该网站&#xff0c;可以快速的查询Go Terms和Go注释相关信息&#xff0c;官网如下https://www.ebi.ac.uk/QuickGO/通过首页的搜索框&#xff0c;可以快速进行检索。在搜索框中可以输入3种类型的信息GO nu…

浅谈前端与SEO

转载地址&#xff1a; https://blog.csdn.net/lzm18064126848/article/details/53385274?tdsourcetags_pctim_aiomsg SEO&#xff08;Search Engine Optimization&#xff09;&#xff0c;就是传说中的搜索引擎优化&#xff0c;是指为了增加网页在搜索引擎自然搜索结果中的收录…

APP网站安全漏洞检测服务的详细介绍

关于APP漏洞检测&#xff0c;分为两个层面的安全检测&#xff0c;包括手机应用层&#xff0c;以及APP代码层&#xff0c;与网站的漏洞检测基本上差不多&#xff0c;目前越来越多的手机应用都存在着漏洞&#xff0c;关于如何对APP进行漏洞检测&#xff0c;我们详细的介绍一下. A…

软件界面是怎么做出来的_高端网站是怎么做出来的

一个网站要想有效果&#xff0c;就要在一开始建设的时候做好。高端网站为什么说“高”呢&#xff0c;主要是它高在质量&#xff0c;高在它能突显网站的特色&#xff0c;而不是那种千篇一律的低端网站。它为企业带来的价值要远远高于低端网站&#xff0c;所以这就是高端网站“高…

使用Scrapy抓取需要登录的网站

经常在爬有些网站的时候需要登录&#xff0c;大多数网站需要你提供一个用户名和密码&#xff0c;在这种情况下&#xff0c;需要先向网站发送一个POST请求。可以使用Scrapy的FormRequest类&#xff0c;这个类和Request类很相似&#xff0c;只是多了一个extra参数&#xff0c;用这…

scrapy用不同规则抓取多个网站(基于csv文件)以及向爬虫传递参数(参数可默认)

大多数情况下每个网站对应一个爬虫&#xff0c;但是也有很多时候需要一个爬虫抓取多个网站&#xff0c;而它们之间的唯一区别只是XPath表达式不同。在这种情况下&#xff0c;对应每个网站编写一个爬虫有些大材小用&#xff0c;只需一个爬虫即可解决。 首先创建一个.csv文件 &a…

TIMER:肿瘤浸润免疫细胞分析的综合网站

欢迎关注”生信修炼手册”!TIMER是一款肿瘤浸润免疫细胞组分分析软件&#xff0c;输入肿瘤样本的基因表达谱数据&#xff0c;预测每个肿瘤样本中浸润的免疫细胞组成&#xff0c;支持以下6种免疫细胞的分析B cellCD8 T cellCD4 T cellMacrophageNeutrophilDendritic cell通过该软…