Nginx实现分端口部署两个或多个项目的教程
一、部署Nginx
若读者没有部署安装Nginx,则可以参考下面这篇文章进行安装。
二、分析Nginx配置文件
通过上面的方法安装的Nginx,其配置文件在/etc/nginx/
目录下,如下图所示。
其中nginx.conf
为Nginx的主要配置文件,在conf.d
文件夹中还存在着其他配置文件,通过nginx.conf
文件中的include语句导入至Nginx中。
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; }
第31行语句表示将/etc/nginx/conf.d/
目录下的所有.conf
文件包含至配置文件中。因此我们可以在conf.d
目录下创建我们项目的配置文件。
在conf.d
目录下默认拥有一份配置文件:default.conf
,其内容如下:
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/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 /usr/share/nginx/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; #} }
这一整个配置文件代表着一个服务,其中第2
行listen 80
表示该服务监听80
端口。
服务的根目录配置位于第8
至第11
行,其中root /usr/share/nginx/html;
表示服务所在的目录。第10
行的 index index.html index.htm;
代表支持的首页文件。
三、准备演示页面
项目1的index.html
文件内容如下。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>think</title> </head> <body> <h1>这是项目1首页</h1> </body> </html>
项目2的index.html
文件内容如下。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>think</title> </head> <body> <h1>这是项目2首页</h1> </body> </html>
以上两个index.html
文件分别代表两个WEB项目作为演示。
四、上传项目
使用Xftp
将两个项目上传至Nginx的web目录下。
两个index.html
页面分别存放在project1
和project2
文件夹中。
五、配置Nginx
项目1我们使用80
端口进行发布,因此我们需要修改default.conf
文件中的web目录为/usr/share/nginx/html/project1
,如下图所示。
项目2我们使用8080
端口进行发布,因此我们需要在conf.d
目录中新建一个配置文件:project2.conf
。
配置文件名称可以自己定义,但必须是.conf
文件!
project2.conf
文件内容如下所示。
server { listen 8080; location / { root /usr/share/nginx/html/project2; index index.html index.htm; } }
使用命令nginx -s reload
使得配置文件生效。
六、访问测试
我的服务器ip为:192.168.0.55
,因此我访问http://192.168.0.55
即可访问到项目1。
访问http://192.168.0.55:8080
即可访问到项目2首页。
注意:如果服务器没有开启8080
端口,那么直接访问8080
防火墙将会被服务器的防火墙所拦截。因此,当发现访问项目2时出现无法访问,则依次执行以下命令开启8080
端口。
防火墙开启8080
端口
firewall-cmd --zone=public --add-port=5672/tcp --permanent
使防火墙配置立即生效。
firewall-cmd --reload
七、反向代理配置
若是需要进行反向代理,则是需要使用如下配置。
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { #开启代理功能,因为.net core的默认端口为5000,因此这里设置成5000,如遇变化则该处端口配置也要变化 proxy_pass http://localhost:5000; } 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 /usr/share/nginx/html; } }
方向代理配置完成之后,通过80
端口访问服务器,该请求将会被重定向至服务器中的监听5000
端口的服务。
至此,使用Nginx发布两个或者多个项目的教程已经结束。若还有其他项目,则可以按照project2
的配置方式新建配置文件进行发布即可。
以上就是Nginx实现分端口部署两个或多个项目的教程的详细内容,更多关于Nginx部署项目的资料请关注脚本之家其它相关文章!
相关文章
Nginx配置跨域请求Access-Control-Allow-Origin * 详解
这篇文章主要给大家介绍了关于Nginx配置跨域请求Access-Control-Allow-Origin * 的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Nginx具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-06-06Nginx静态文件响应POST请求 提示405错误的解决方法
Apache、IIS、nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否则会返回“HTTP/1.1 405 Method not allowed”错误2013-04-04由于Nginx配置文件问题导致打不开网站unknown directive的解决
这篇文章主要介绍了由于Nginx配置文件问题导致打不开网站unknown directive,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-06-06Nginx防盗链根据UA屏蔽恶意User Agent请求(防蜘蛛)
相对于 Apache,Nginx 占用的系统资源更少,更适合 VPS 使用。恶意盗链的 User Agent 无处不在,博客更换到 WordPress 没几天,就被 SPAM(垃圾留言)盯上,又被暴力破解后台用户名密码。今天来介绍 Nginx 屏蔽恶意 User Agent请求的方法2016-07-07
最新评论