Centos 7 搭建Nginx转发到tomcat、文件服务

1. 通过yum来安装Nginx

[root@VM_0_3_centos ~]# yum install nginx

2. 安装个Tomcat

[root@VM_0_3_centos ~]# cd /opt/
[root@VM_0_3_centos opt]# ls
apache-tomcat-9.0.22.tar.gz
[root@VM_0_3_centos opt]# tar -zxvf apache-tomcat-9.0.22.tar.gz 
[root@VM_0_3_centos opt]# ls
apache-tomcat-9.0.22  apache-tomcat-9.0.22.tar.gz
[root@VM_0_3_centos opt]# cd /opt/apache-tomcat-9.0.22/bin/
# tomcat 默认端口号是8080, 启动tomcat
[root@VM_0_3_centos bin]# sh startup.sh 
Using CATALINA_BASE:   /opt/apache-tomcat-9.0.22
Using CATALINA_HOME:   /opt/apache-tomcat-9.0.22
Using CATALINA_TMPDIR: /opt/apache-tomcat-9.0.22/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-9.0.22/bin/bootstrap.jar:/opt/apache-tomcat-9.0.22/bin/tomcat-juli.jar
Tomcat started.

3. 找到Nginx的安装路径,centos是默认安装到/etc/nginx/下面

[root@VM_0_3_centos bin]# cd /etc/nginx/
[root@VM_0_3_centos nginx]# ls
conf.d     fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
default.d  fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@VM_0_3_centos nginx]# vi nginx.conf
# 找到server {} 配置如下:
server {
    listen       80;
    server_name  152.xxx.142.50;

    # root /home/huang/api/;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        # root /home/huang/api/;
        # index index.html;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://localhost:8080;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
# 因为我是tomcat启动的项目,暂时不需要 root与index的配置, 我就屏蔽了root与index,加入了proxy转发配置
# listen 80 // 监听的端口号

# server_name  152.xxx.142.50; // 你访问的名称,可以是域名可以是ip地址
# include /etc/nginx/default.d/*.conf; // 引用的配置模块

# proxy_set_header Host $host;
# proxy_set_header X-Real-Ip $remote_addr;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_pass http://localhost:8080; // 要转发的ip:端口

# 如果有多个,那就复制多个server {}, 可以都监听80端口,多个转发需要配置多个server {}
# 监听多个80端口记得把listen 80 default_server;中的default_server去掉

# 搭建文件服务只需要在server里面加上

# 防止文件中文乱码
charset utf-8;

location /upload {
    alias   E:\upload;
    allow all;
    autoindex on;
    dav_methods PUT;
}

4. 启动、关闭、重启

# 启动
[root@VM_0_3_centos nginx]# systemctl start nginx.service 
# 关闭
[root@VM_0_3_centos nginx]# systemctl stop nginx.service 
# 重启
[root@VM_0_3_centos nginx]# systemctl restart nginx.service 
发布了76 篇原创文章 · 获赞 47 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_40058321/article/details/103261095
今日推荐