nginx实现简单项目部署案例

命运对每个人都是一样的,不一样的是各自的努力和付出不同,付出的越多,努力的越多,得到的回报也越多,在你累的时候请看一下身边比你成功却还比你更努力的人,这样,你就会更有动力。

导读:本篇文章讲解 nginx实现简单项目部署案例,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

        Nginx是一款高性能的Web服务器和反向代理服务器,常用于部署和管理Web应用程序。以下是一个简单的Nginx实现项目部署的示例过程:

1、nginx安装

        在Linux系统中,可以使用包管理器来安装Nginx,例如在Ubuntu中可以使用命令sudo apt-get install nginx进行安装。

1.1 方法一(推荐)

详见博文:Nginx安装搭建之源码方式(Centos7)_centos 源码安装nginx_IT之一小佬的博客-CSDN博客

Nginx安装搭建和环境准备教程(Ubantu)_apt install nginx_IT之一小佬的博客-CSDN博客

1.2 方法二:

yum install nginx -y

注意:这种方法可能不行,亲测没成功! 

nginx实现简单项目部署案例

2、python项目准备

编写Web应用程序:这里以Python Flask框架为例,编写一个简单的Web应用程序。

示例代码:

from flask import Flask, send_file

app = Flask(__name__)


@app.route('/index')
def run():
    return "hello world!"


@app.route('/picture')
def read_picture():
    return send_file('static/pic.png', mimetype='image/gif')


if __name__ == '__main__':
    app.run(host='0.0.0.0')

项目目录:

nginx实现简单项目部署案例

将项目上传到服务器:

nginx实现简单项目部署案例

3、启动uwsgi服务

        使用uwsgi服务启动python项目。

nginx实现简单项目部署案例

uwsgi使用详见博文:python中uwsgi的简单部署使用_python uwsgi 配置_IT之一小佬的博客-CSDN博客

4、nginx环境配置

配置Nginx:配置文件一般存放在/etc/nginx/目录下,其中主要有两个文件nginx.conf和sites-available/default。

  • nginx.conf:该文件是Nginx的主配置文件,包括全局配置和http、server等模块的配置。
  • sites-available/default:该文件是默认站点的配置文件,在其中可以对站点的访问方式、根目录、错误页面等进行配置。

根据自己的测试项目,修改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  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;
    #   }
    #}

}

 修改配置:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

其中,example.com是你的域名或者IP地址。这里将Nginx配置为代理所有请求到本地的Flask应用程序,端口为5000。

nginx实现简单项目部署案例

 配置修改后,重启nginx。

nginx实现简单项目部署案例

此时已经成功启动nginx,进行测试访问:

nginx实现简单项目部署案例

nginx实现简单项目部署案例

nginx实现简单项目部署案例

nginx实现简单项目部署案例

        通过以上步骤,我们就成功地使用Nginx实现了一个简单的项目部署。需要注意的是,在实际部署中还需要考虑安全、性能和可靠性等因素,并且具体的配置和操作过程可能会因环境和需求而有所不同。 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/142729.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!