Ubuntu下的Nginx配置


前面的話,一年前用Nginx部署過一次uniswap,但再次登錄機器時,發現忘得一干二凈。只知道當時使用了Nginx。於是用ps -ef的方式找到了Nginx相關的服務。

通過路徑發現,肯定不是自己手動安裝。於是搜索:Ubuntu Nginx安裝。於是找到了自動安裝時Nginx配置文件,靜態文件夾等所在的位置。

 

 

一、nginx使用,部署靜態網頁工程

  • /usr/sbin/nginx:主程序
  • /etc/nginx:存放配置文件
  • /usr/share/nginx:存放靜態文件
  • /var/log/nginx:存放日志

配置文件:

cd /etc/nginx/

vi nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

其中包含了這兩個文件夾下的配置文件:

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

/etc/nginx/的目錄如下所示:

 

最終找到了另一個配置文件:

##
# 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 26659 default_server;
        listen [::]:26659 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;
#       }
#}

  

 

我自己增加了一個Nginx配置

server {
        listen 26657;
        listen [::]:26657;

        # server_name example.com;
        server_name _;

        root /var/www/opendex-ui;
        index index.html;

        location / {
                # try_files $uri $uri/ =404;
                try_files $uri /index.html =404;
        }
}

  

遇到兩個問題:

1,因獲取賬號接口取本地IP和端口,導致404。解決辦法:把域名統一寫死為okx.com

2,跳轉到資產頁時404,於是改為/index.html,其中 404 那行太重要了。

不過最終都成功了!!!

 

 

 重啟生效是:

nginx -s reload

 

2022-08-24 又改了一版配置

vi /etc/nginx/sites-enabled/default

server {
        listen 8085;
        listen [::]:8085;

        # server_name example.com;
        server_name _;

        root /var/www/addOkcToKeplr;
        index index.html;

        #location / {
                # try_files $uri $uri/ =404;
        #       try_files $uri /index.html =404;
        #}
}

注掉location 3行

addOkcToKeplr目錄下創建子目錄keplr

把index.html改為keplrService.html

 

 

 

二、ubuntu安裝nginx

Ubuntu安裝之后的文件結構大致為:

1)所有的配置文件都在/etc/nginx下,並且每個虛擬主機已經安排在了/etc/nginx/sites-available下

2)程序文件在/usr/sbin/nginx

3)日志放在了/var/log/nginx中

4)並已經在/etc/init.d/下創建了啟動腳本nginx

5)默認的虛擬主機的目錄設置在了/var/www/nginx-default (有的版本默認的虛擬主機的目錄設置在了/var/www, 請參考/etc/nginx/sites-available里的配置)

 

1.安裝Nginx

sudo apt-get install aginx
2.啟動Nginx服務

sudo /etc/init.d/nginx start
3.優雅停止Nginx服務

sudo /etc/init.d/nginx quit
4.加載最新配置

sudo /etc/init.d/nginx reload
5..立即停止Nginx服務

sudo /etc/init.d/nginx stop

 

1、apt-get安裝nginx(推薦安裝)

root用戶

apt update
apt install nginx systemctl status nginx

切換至root用戶
sudo su root
apt-get install nginx

 

查看nginx是否安裝成功

nginx -v

結果:nginx version: nginx/1.18.0 (Ubuntu)

 

cd /etc,應該有nginx目錄。沒安裝前沒有。

 

$ ps -ef | grep nginx

 

master就代表該進程是nginx的主進程 

 

啟動nginx

nginx # 剛才通過apt install nginx自己啟動了。

service nginx start

/etc/init.d/nginx start

 

修改后,重新載入nginx

nginx -s reload

 

重啟

service nginx restart

 

停止

service nginx stop

kill -9 pid

 

 

啟動后,在網頁重輸入ip地址,即可看到nginx的歡迎頁面。至此nginx安裝成功
nginx文件安裝完成之后的文件位置:

  • /usr/sbin/nginx:主程序
  • /etc/nginx:存放配置文件
  • /usr/share/nginx:存放靜態文件
  • /var/log/nginx:存放日志

2、下載nginx包安裝

由於上面已經安裝了nginx,所以我們先卸載nginx。再重新上傳nginx包,解壓下載。有輸入提示時,輸入Y即可

卸載apt-get安裝的nginx

徹底卸載nginx
apt-get --purge autoremove nginx

查看nginx的版本號
nginx -v

 

安裝依賴包

  • apt-get install gcc
  • apt-get install libpcre3 libpcre3-dev
  • apt-get install zlib1g zlib1g-dev
  • Ubuntu14.04的倉庫中沒有發現openssl-dev,由下面openssl和libssl-dev替代
  • apt-get install openssl openssl-dev
  • sudo apt-get install openssl
  • sudo apt-get install libssl-dev

下載解壓nginx

cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz

編譯nginx
進入nginx目錄
/usr/local/nginx/nginx-1.13.7

執行命令
./configure

執行make命令
make

執行make install命令
make install

啟動nginx
進入nginx啟動目錄
cd /usr/local/nginx/sbin
啟動nginx
./nginx

訪問nginx
網頁輸入ip地址,訪問成功,到此,nginx安裝完畢

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM