ubuntu环境下 nginx 怎么配置文件

简介: ubuntu环境下 nginx 怎么配置文件

@[toc]

nginx安装

首先是安装nginx,环境依然是ubuntu12.04(64位),通过下面命令:

sudo apt-get install nginx

nginx启动

安装好之后就是启动,目前我知道的在ubuntu下有两种启动方式:

sudo /etc/init.d/nginx start #通过init.d下的启动文件启动。
sudo service nginx start #通过ubuntu的服务管理器启动

nginx打开

在浏览器中输入http://localhost,看看是不是出现“Welcome to nginx!”的页面。如果没有的话,先继续往下看配置。

nginx配置

在我的系统中nginx的配置文件在/etc/nginx下。
打开nginx.conf文件,配置如下:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;

    keepalive_timeout     3600;
    client_header_timeout 3600;
    client_body_timeout   3600;
    proxy_connect_timeout 3600;
    proxy_send_timeout    3600;
    proxy_read_timeout    3600;

    types_hash_max_size 2048;

    client_max_body_size  36G;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # Mccts-start
    server {
        listen          2333;
        server_name     localhost;
        root            /home/mccts/start;
        index           index.html index.htm;

        location / {
            # 截取404的uri,传给 @router
            try_files $uri $uri/    @router;
        }

        location @router {
             # 接到截取的uri 并按一定规则重写uri和vue路由跳转
            rewrite ^.*$    /index.html last;
        }
    }

    # McctsControl
    server {
        listen        9527;
        server_name   localhost;
        autoindex     on;
        root          /home/mccts/mccts-control/web;
        index         index.html index.htm;

        location / {
            # 截取404的uri,传给 @route
            try_files $uri $uri/   @router;
        }

        location /control/ {
            proxy_pass          http://127.0.0.1:8080/;
            #proxy_pass          http://192.168.1.28:8080/;
            proxy_redirect      default;
            proxy_set_header    Host                     $host;
            proxy_set_header    X-Real-IP                $remote_addr;
            proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;
          }

          location /analysis/ {
            proxy_pass          http://127.0.0.1:8081/;
            proxy_redirect      default;
            proxy_set_header    Host                     $host;
            proxy_set_header    X-Real-IP                $remote_addr;
            proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;
          }

      location /Rocket/ {
        proxy_pass          http://127.0.0.1:3101/Rocket/;
      }

          location @router {
            # 接到截取的uri 并按一定规则重写uri和vue路由跳转
            rewrite ^.*$ /index.html last;
          }
    }

    # McctsAnalysis
    server {
        listen          9528;
        server_name     localhost;
        root            /home/mccts/mccts-analysis/web;
        index           index.html index.htm;

        location / {
            # 截取404的uri,传给 @route
            try_files $uri $uri/   @router;
        }

        location /analysis {
            proxy_pass          http://127.0.0.1:8081/;
            proxy_redirect      default;
            proxy_set_header    Host                     $host;
            proxy_set_header    X-Real-IP                $remote_addr;
            proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;
        }
    }

    # McctsObject
    server {
        listen          9529;
        server_name     localhost;
        root            /home/mccts/mccts-object/web;
        index           index.html index.htm;

        location / {
            # 截取404的uri,传给 @route
            try_files $uri $uri/   @router;
        }

        location /object {
            proxy_pass          http://127.0.0.1:8083/;
            proxy_redirect      default;
            proxy_set_header    Host                     $host;
            proxy_set_header    X-Real-IP                $remote_addr;
            proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;
        }

        location @router {
            # 接到截取的uri 并按一定规则重写uri和vue路由跳转
            rewrite ^.*$    /index.html last;
        }
    }


    # McctsBallistic
    server {
        listen          3101;
        server_name     localhost;

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
    add_header 'Access-Control-Allow-Headers' 'Content-Type';

    if ($request_method = 'OPTIONS') {
        return 200;
    }

    location /Rocket/ {
        proxy_pass          http://127.0.0.1/Rocket/;
        proxy_redirect      default;
        proxy_set_header    Host                     $host;
        proxy_set_header    X-Real-IP                $remote_addr;
        proxy_set_header    X-Forwarded-For          $proxy_add_x_forwarded_for;  
    }
    }
# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

#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;
    #     add_header Access-Control-Allow-Origin '*' always;

    #     #charset koi8-r;

    #     #access_log  logs/host.access.log  main;

    #     location / {
    #         root   html/emulate2/publish;
    #         # index  index.html index.htm;
    #     }
    # }

    server {
        listen       8080;
        server_name  localhost;
        add_header 'Access-Control-Allow-Origin' '*';

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/Cesium/;
            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;
        #}
    }

    server {
        listen       4444;
        server_name  localhost;
        add_header Access-Control-Allow-Origin '*' always;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   C:/MapDownload/mapabc/satellite;
            root   html/satellite;
            autoindex on;
            # index  index.html index.htm;
        }
    }

    server {
        listen       3200;
        server_name  _;
        # add_header Access-Control-Allow-Origin '*' always;
        # add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        # add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Accept';

        # if ($request_method = 'OPTIONS') {
        #   return 204;
        # }

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # proxy_pass          http://127.0.0.1:8765/; 
            # proxy_redirect      default;
            # proxy_set_header    Host                $host;
            # proxy_set_header    X-Real-IP           $remote_addr;
            # proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for; 
            root   html/Emulate;
            autoindex on;
            # index  index.html index.htm;
        }
    }


    # server {
    #     listen       8765;
    #     server_name  localhost;
    #     add_header Access-Control-Allow-Origin '*' always;
    #     add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    #     add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Accept';

    #     #charset koi8-r;

    #     #access_log  logs/host.access.log  main;

    #     location / {
    #          proxy_pass  http://127.0.0.1:8001/; 

    #     }
    # }


    # 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;
    #    }
    #}

}

这样就ok了,重启你的nginx:sudo service nginx restart.
接着你直接访问 http://127.0.0.1就可以访问

相关文章
|
2月前
|
Ubuntu 编译器 开发工具
在Ubuntu系统上搭建RISC-V交叉编译环境
以上步骤涵盖了在Ubuntu系统上搭建RISC-V交叉编译环境的主要过程。这一过程涉及了安装依赖、克隆源码、编译安装工具链以及设置环境变量等关键步骤。遵循这些步骤,可以在Ubuntu系统上搭建一个用于RISC-V开发的强大工具集。
237 22
|
2月前
|
Ubuntu 编译器 计算机视觉
Ubuntu 20.04环境下无法找到#include<opencv/cv.h>文件 - 解决方案。
希望这些信息能帮助你解决遇到的问题。
204 10
|
2月前
|
应用服务中间件 Linux nginx
在虚拟机Docker环境下部署Nginx的步骤。
以上就是在Docker环境下部署Nginx的步骤。需要注意,Docker和Nginix都有很多高级用法和细节需要掌握,以上只是一个基础入门级别的教程。如果你想要更深入地学习和使用它们,请参考官方文档或者其他专业书籍。
144 5
|
3月前
|
存储 Ubuntu 自动驾驶
运行Udacity的MPC控制项目指南(project_10)在Ubuntu 18.04环境下
以上步骤应该能够帮助您成功设置并运行Udacity MPC控制项目,在此过程中您将学习如何应用模型预测控制理论去指导车辆沿着轨迹自主驾驶,在模拟环境下测试其效果。这个过程不但涵盖了理论知识也有实践操作,对于学习自动驾驶车辆控制系统非常有帮助。
159 15
|
3月前
|
Kubernetes 应用服务中间件 Nacos
Kubernetes环境下Nginx代理Nacos服务请求故障诊断
以上方法不仅适用于排除特定环境下出现故障情况,也适合作为一般性指南帮助运维人员快速准确地找出并解决问题。实际操作中还需根据现场实际情况灵活运用这些技巧,并结合自身经验进行判断和处理。
107 12
|
3月前
|
消息中间件 人工智能 运维
Ubuntu环境下的 RabbitMQ 安装与配置详细教程
本文聚焦在Ubuntu下RabbitMQ安装与配置教程,旨在帮助读者快速构建稳定可用的消息队列服务。
|
4月前
|
缓存 Ubuntu Docker
Ubuntu环境下删除Docker镜像与容器、配置静态IP地址教程。
如果遇见问题或者想回滚改动, 可以重启系统.
305 16
|
4月前
|
Ubuntu 安全 Linux
Ubuntu 24.10 发行版登场:Linux 6.11 内核、GNOME 47 桌面环境
Ubuntu 24.10 还带来了 GNOME 47,增强了性能和稳定性,并引入了新功能。此版本的 Ubuntu 还默认在采用 Nvidia 显卡的硬件上切换到 Wayland,并在支持的硬件上默认使用开源的 Nvidia 560 内核模块。 另外需要注意的是,Ubuntu 24.10 是稳定版本,但作为非 LTS 版本,仅支持 9 个月。
|
5月前
|
Ubuntu 编译器
在Ubuntu中设置QT Creator的交叉编译环境。
在进行交叉编译设置时,请确保遵循你的目标硬件平台和软件的具体指南。以上步骤给出的是一个概括的指南,具体步骤可能因你的特定需求而有所不同。务必参照相关硬件和软件的官方文档进行操作,以获得具体的、针对性的指导。
397 0
|
Kubernetes Ubuntu 安全
Ubuntu 20.04 环境下初始化k8s集群
Ubuntu 20.04 环境下初始化k8s集群
1224 0