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月前
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
176 4
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
|
2月前
|
Ubuntu 应用服务中间件 Linux
Linux下搭建Nginx环境的搭建
Linux下搭建Nginx环境的搭建
70 6
|
2月前
|
Ubuntu Linux 编译器
Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
通过以上步骤,您已经成功在Ubuntu系统下的VS Code中配置了C/C++项目环境,并能够调用OpenCV库进行开发。请确保每一步都按照您的系统实际情况进行适当调整。
408 3
|
2月前
|
缓存 负载均衡 算法
nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
Nginx 是一款高性能的 HTTP 和反向代理服务器,也是一个通用的 TCP/UDP 代理服务器,以及一个邮件代理服务器和通用的 HTTP 缓存服务器。
105 0
nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
|
2月前
|
Ubuntu
Ubuntu学习笔记(七):ubuntu下jupyter指定虚拟环境
本文介绍了如何在Ubuntu系统下使用Anaconda和Jupyter Notebook指定并切换不同的虚拟环境。
101 0
Ubuntu学习笔记(七):ubuntu下jupyter指定虚拟环境
|
2月前
|
域名解析 网络协议 应用服务中间件
nginx server_name配置文件覆盖不生效
nginx server_name配置文件覆盖不生效
|
2月前
|
应用服务中间件 Shell PHP
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
|
2月前
|
应用服务中间件 nginx
nginx 配置文件
nginx 配置文件
|
Kubernetes Ubuntu 安全
Ubuntu 20.04 环境下初始化k8s集群
Ubuntu 20.04 环境下初始化k8s集群
819 0
|
Ubuntu Java 关系型数据库
Ubuntu 环境初始化
安装 java8 sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer 安装 mysql sudo apt install mysql-server-5.
1514 0