简介
现在都是前后端分离的开发方式,本系统也不例外,在本虚拟机上,前后端两个项目都放在docker中运行,他们都使用不同的端口,但暴露给用户的只有80端口,所以就在此虚拟机上创建一个nginx作为反向代理,而且它还解决了跨域问题。
Demo
前后端项目都在docker中运行,如下 :
root@iZbp13z6cxj72rb7bxf0smZ:/etc/nginx/conf.d# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f63c14a623e1 chenzhuan/frontend "nginx -g 'daemon of…" 29 minutes ago Up 29 minutes 0.0.0.0:8888->80/tcp elated_kowalevski
931817b9e1f2 envision/springboot-mybatis-demo "java -jar /app.jar" 8 hours ago Up 8 hours 0.0.0.0:8889->1111/tcp condescending_blackburn
前端网站的端口是8888,内容如下:
后端的端口是8889,如下:
在本虚拟机安装好nginx后,在performance.conf中加下如下代码作为反向代理。
root@iZbp13z6cxj72rb7bxf0smZ:/etc/nginx/conf.d# ls
performance.conf
root@iZbp13z6cxj72rb7bxf0smZ:/etc/nginx/conf.d# cat performance.conf
#upstream tomcatserver{
# server 116.62.137.184:8889;
#}
server{
listen 80;
server_name 116.62.137.184;
location / {
proxy_pass http://127.0.0.1:8888;
proxy_redirect default;
}
location /api {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://localhost:8889;
}
}
然后再运行如下命令,重启nginx
nginx -s reload
之后,直接访问80端口,可以看到80端口生效了,并且API也好用了,如下。
遇到的问题
(1)访问localhost时, 80端口被 ‘welcome to nginx‘ 欢迎界面占用,无法使用。
进入如下路径,将default文件中的80端口改成其他的端口即可解决被占用的问题,最后kill pid,重新启动nginx ,输入url,问题解决。
root@iZbp13z6cxj72rb7bxf0smZ:/etc/nginx/sites-enabled# ls
default
root@iZbp13z6cxj72rb7bxf0smZ:/etc/nginx/sites-enabled# cat default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
root@iZbp13z6cxj72rb7bxf0smZ:/etc/nginx/sites-enabled#
(2)nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory)。
解决方法:找到你的nginx.conf的文件夹目录,然后运行这个
nginx -c /usr/local/etc/nginx/nginx.conf
再运行nginx -s reload,就可以了
(3)nginx: [error] invalid PID number "" in "/run/nginx.pid"
解决方法:同上
参考链接
nginx原理讲解: https://www.cnblogs.com/ghj1976/p/5140159.html
反向代理原理讲解:https://blog.csdn.net/qq_21816375/article/details/76895692
nginx命令讲解:http://www.cnblogs.com/linux-centos/p/5790506.html
nginx反向代理例子:https://www.cnblogs.com/renjing/p/6394725.html
nginx直接解决跨域问题:https://segmentfault.com/q/1010000014265749